- Fragment回退栈
在AFragment文件中
package com.example.myapplication.Fragment;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.myapplication.R;
public class AFragment extends Fragment {
private TextView mTvTitle;
private Button mBtnChange,mBtnReset;
private BFragment bFragment;
public AFragment(){
}
//旧版写法
public static AFragment newInstance(String title){
AFragment aFragment = new AFragment();
Bundle bundle = new Bundle();
bundle.putString("title",title);
aFragment.setArguments(bundle);
return aFragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
//类似与在Fragment中setContentView()
View view = inflater.inflate(R.layout.fragment_a,container,false);
Log.d("AFragment","---AFragment---");
return view;
}
//像findViewById这种操作就放到这里
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mTvTitle =view.findViewById(R.id.tv_title);
mBtnChange = view.findViewById(R.id.btn_change);
mBtnReset = view.findViewById(R.id.btn_reset);
mBtnChange.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (bFragment == null){
bFragment = new BFragment();
}
Fragment fragment = getFragmentManager().findFragmentByTag("a");
if (fragment != null){
//先隐藏,再添加,这样回退时回收了表层的视图,底下的视图就可以看见了,也不需要重新加载浪费时间
getFragmentManager().beginTransaction().hide(fragment).add(R.id.f1_container,bFragment).addToBackStack(null).commitAllowingStateLoss();
}
else {
// addToBackStack可以使我们按下返回键的时候只返回上一层Fragment,不退回主页面,commit之前放入了回退栈
getFragmentManager().beginTransaction().replace(R.id.f1_container,bFragment).addToBackStack(null).commitAllowingStateLoss();
}
}
});
mBtnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTvTitle.setText("我是新文字");
}
});
if (getArguments()!=null){
mTvTitle.setText(getArguments().getString("title"));
}
// mTvTitle.setText(title);
// if (getActivity() !=null){
//
// }else{
//
// }
}
}
视图如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/btn_change"
android:text="更换为BFragment"
android:textAllCaps="false"/>
<Button
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/btn_reset"
android:text="更换TextView的文字内容"
android:textAllCaps="false"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="20sp"
android:text="我是AFragment"
android:gravity="center"
android:id="@+id/tv_title"/>
</LinearLayout>