繁体   English   中英

片段和Android生命周期

[英]Fragments and android life cycle

我的Android应用程式有些奇怪。

如果我运行主要活动,然后经过几个片段,那么在隐藏/取消隐藏应用程序后,我会看到主要活动。 如果按“后退”,它将显示隐藏之前显示的片段。 但有时应用程式会显示取消隐藏后的最后片段。 如果可能,如何使我的应用每次显示最后一个片段? 或者如何使我的应用保存状态?

据我了解,您想要在按下“后退”按钮时显示最后一个片段 为此,您可以依靠addToBackStack方法来处理Back按下。 来自Google代码段@ Fragments

Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

注意:在这种情况下,newFragment是您的最后一个片段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM