[英]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.