我正在尝试创建自定义标签导航。在每个选项卡上单击时,它将替换屏幕中的片段。在MainActivity.java上,我正在为每个片段创建一个副本(我想以此方式,我可以在任何地方使用每个片段的一个副本)。
public static WhereRootFragment whereRootFragment;
public static FeedRootFragment feedRootFragment;
public static WhereMainFragment whereMainFragment;
public static FeedMainFragment feedMainFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
.....
whereRootFragment=new WhereRootFragment();
feedRootFragment=new FeedRootFragment();
whereMainFragment=new WhereMainFragment();
feedMainFragment=new FeedMainFragment();
ClickHandler.handleClick(MainActivity.this, llWhere);
}
单击选项卡按钮时,它将旧片段替换为当前片段(handleClick方法)。
switch (tag) {
case TAG_WHERE:
MainActivity.ivWhere.setImageDrawable(a.getResources().getDrawable(
R.drawable.where_black));
// Fragment fragment_where_root=new WhereRootFragment();
FragmentTransaction transaction_where = ((FragmentActivity) a).getSupportFragmentManager().beginTransaction();
transaction_where.replace(R.id.root_frame, MainActivity.whereRootFragment);
transaction_where.addToBackStack(null);
transaction_where.commit();
break;
case TAG_FEED:
MainActivity.ivFeed.setImageDrawable(a.getResources().getDrawable(
R.drawable.feed_black));
// Fragment fragment_feed_root=new FeedRootFragment();
FragmentTransaction transaction_feed = ((FragmentActivity) a).getSupportFragmentManager().beginTransaction();
transaction_feed.replace(R.id.root_frame, MainActivity.feedRootFragment);
transaction_feed.addToBackStack(null);
transaction_feed.commit();
break;
WhereRootFragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/* Inflate the layout for this fragment */
View view = inflater.inflate(R.layout.fragment_where_root, container, false);
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
transaction.replace(R.id.where_root_frame, MainActivity.whereMainFragment);
transaction.addToBackStack(null);
transaction.commit();
return view;
}
WhereMainFragment
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_where_main,
container, false);
return rootView;
}
结果:
[StartApp]-> [1.Where:showing]-> [2.Feed:showing]-> [3.Where:不显示]-> [4.Feed:显示保存的状态]-> [5。另一个片段:按预期为空]-> [6.Feed:未显示]
问:如何解决这个问题?如何使片段保存其状态?
最佳答案
WhereRootFragment
由属于该活动的片段管理器管理。之所以发生此问题,是因为使用了相同的片段管理器来管理WhereMainFragment
,它是WhereRootFragment
的子级。WhereMainFragment
应该由属于WhereRootFragment
的片段管理器管理。要在片段中获取子片段管理器,请使用Fragment.getChildFragmentManager。
WhereRootFragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/* Inflate the layout for this fragment */
View view = inflater.inflate(R.layout.fragment_where_root, container, false);
// Use child fragment manager to manage WhereMainFragment
FragmentTransaction transaction = getChildFragmentManager()
.beginTransaction();
transaction.replace(R.id.where_root_frame, MainActivity.whereMainFragment);
transaction.addToBackStack(null);
transaction.commit();
return view;
}
关于java - 安卓 fragment 不保存状态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25499207/