天哥视频笔记
Fragment 回退栈
java 代码:AFragment.class
public class AFragment extends Fragment {
private TextView mTvTitle;
private Button btn_change;
private Button btn_reset;
private BFragment bFragment;
public static AFragment newInstance(String title){
AFragment fragment=new AFragment();
Bundle bundle=new Bundle();
bundle.putString("title",title);
fragment.setArguments(bundle);
return fragment;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_a,container,false);
Log.d("AFragment","-------onCreateView-------");
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mTvTitle=(TextView)view.findViewById(R.id.tv_title);
btn_change = (Button)view.findViewById(R.id.btn_change);
btn_reset = (Button) view.findViewById(R.id.btn_reset);
btn_change.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(bFragment == null){
bFragment=new BFragment();
}
//判断aFragment是否存在
Fragment fragment=getFragmentManager().findFragmentByTag("a");
if(fragment!=null){
//aFragment存在就先隐藏aFragment,然后再添加bFragment
getFragmentManager().beginTransaction().hide(fragment).add(R.id.fl_container,bFragment).addToBackStack(null).commitAllowingStateLoss();
}else{
//在commit之前把fragment添加到回退栈
//replace=先remove+后add
getFragmentManager().beginTransaction().replace(R.id.fl_container,bFragment).addToBackStack(null).commitAllowingStateLoss();
}
}
});
btn_reset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mTvTitle.setText("我是新文字");
}
});
if(getArguments()!=null){
//获取参数
mTvTitle.setText(getArguments().getString("title"));
}
}
}
关键代码1
1. 在container_activity中定义了aFragment的标签为a,所以这里通过findFragmentTag("a")获取aFragment
2. 判断afragment是否存在,若存在先调用hide方法隐藏,在添加bfragment,添加bFragment后需要调用addToBackStack(null)把fragment放到返回栈,否则返回时不会退到aFragment,而是退到container_activity
3.若不存在,直接调用replace方法更换bFragment,replace方法=先remove(aFragment)+add(bFragment)
Fragment fragment=getFragmentManager().findFragmentByTag("a");
if(fragment!=null){
getFragmentManager().beginTransaction().hide(fragment).add(R.id.fl_container,bFragment).addToBackStack(null).commitAllowingStateLoss();
}else{
//在commit之前把fragment添加到回退栈
//replace=先remove+后add
getFragmentManager().beginTransaction().replace(R.id.fl_container,bFragment).addToBackStack(null).commitAllowingStateLoss();
}
xml 代码:fragment_a.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<Button
android:id="@+id/btn_change"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="更换为BFragment"
android:textAllCaps="false"/>
<Button
android:id="@+id/btn_reset"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="更换为TextView的文字内容"
android:textAllCaps="false"/>
<TextView
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="20sp"
android:text="我是AFragment"
android:gravity="center"/>
</LinearLayout>
java 代码:ContentActivity.class
public class ContainerActivity extends AppCompatActivity {
private AFragment aFragment;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
//R.id.fl_container:装在fragment的容器
//aFragment:要加载的fragment
// aFragment=new AFragment();
//调用Fragment的一个静态方法newInstance
aFragment=AFragment.newInstance("我是参数");
//把AFragment添加到Activity中,记得调用commit
//给fragmenta加一个标识"a",方便之后调用它
getSupportFragmentManager().beginTransaction().add(R.id.fl_container,aFragment,"a").commitAllowingStateLoss();
}
}
xml 代码:content_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/fl_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>