简体   繁体   English

android恢复在堆栈中的片段的实例状态

[英]android restore instance state of a fragment that is in backstack

Hi I have an activity that has two fragments. 嗨,我有一个活动,有两个片段。

  1. Fragment A 片段A
  2. Fragment B 片段B

    • Fragment A has an EditText and a ListView. 片段A具有一个EditText和一个ListView。
    • Once I enter something in the EditText and hit Enter , I populate the ListView. 在EditText中输入内容并按Enter后 ,将填充ListView。
    • Now Fragment A's ListView is populated with data. 现在,片段A的ListView中已填充数据。
    • Clicking any item on the ListView of fragment A will send the user to fragment B 单击片段A的ListView上的任何项目都会将用户带到片段B
    • At this point I am replacing fragment A with fragment B 此时,我正在用片段B替换片段A
    • So when the user hits back button he comes back to fragment A 因此,当用户单击“后退”按钮时,他返回到片段A

Now the problem is if the user is in fragment B and config changes occur like screen rotation etc more than once then my that my ListView is empty because my arraylist is null. 现在的问题是,如果用户位于片段B中,并且配置更改(例如屏幕旋转等)发生了多次,那么我的ListView为空,因为我的arraylist为null。

Note that I am using onSavedInstanceState in fragment A and fragment B. If my current fragment is fragment A and config changes occur then there is no problem of restoring the state since in onCreateView I am getting the arraylist from bundle. 请注意,我在片段A和片段B中使用onSavedInstanceState。如果我当前的片段是片段A,并且发生配置更改,那么恢复状态没有问题,因为在onCreateView中,我从捆绑软件中获取了arraylist。

I know the reason why my arraylist is null when I comeback from fragment B to fragment A which is in backstack previously.When fragment A is in backstack the only method that is being called is onSaveInstanceState so after the first config change my arraylist field is null as I couldnt assign my arraylist stored in savedInstateState bundle to the arraylist field. 我知道为什么我从片段B返回到先前在后堆栈中的片段A时arraylist为空的原因。当片段A在后堆栈中时,唯一被调用的方法是onSaveInstanceState,因此在第一次配置更改后,我的arraylist字段为null因为我无法将存储在saveInstateState捆绑包中的arraylist分配给arraylist字段。

I do not want to use android:configchanges attribute in my manifest. 我不想在清单中使用android:configchanges属性。

My Question is how can I restore the state of a fragment that is in backstack. 我的问题是我该如何恢复堆栈中的碎片状态。

I achieved the goal of retaining the state of fragment which is in backstack by passing all the details of that fragment to main activity before it is added to backstack and saving the state in main activity's savedInstance state and whenever backstack changes and previous fragment is visible I am passing the saved instance state of that fragment to it from main activity and this way I am retaining the state. 我达到了保留片段在堆栈中的状态的目的,方法是将该片段的所有详细信息传递给主活动,然后再将其添加到堆栈中,并将该状态保存到主活动的saveInstance状态中,并且只要堆栈发生变化且上一个片段可见时,我正在从主要活动中将该片段的已保存实例状态传递给它,因此我保留了该状态。 I am not sure if this the right approach. 我不确定这是否正确。

I have gone through your scenario. 我已经了解了您的情况。 Basically you are doing right job when you are adding arraylist in savedinstanceState of your fragment but according to me you are doing little mistake in your activity. 基本上,当您在片段的saveedinstanceState中添加arraylist时,您做的不错,但是根据我的说法,您的活动没有犯任何错误。

As when orientation changes Activity call onDestroy then onCreate in that scenario if you are not checking that either your fragment is already added or not then it will replace it with new one. 当方向改变时,在这种情况下,如果您不检查片段是否已添加,Activity会先调用onDestroy然后再调用onCreate,然后它将用新片段替换它。 Here is code you should put in onCreate() 这是您应该放在onCreate()中的代码

 FragmentManager fragmentManager = getFragmentManager();
    Fragment fragment = fragmentManager.findFragmentByTag("FragmentA");
    if(fragment==null){
        fragmentManager.beginTransaction().add(R.id.container, new FrgamentA(), "FragmentA").commit();
    }

Another point is when you want your first fragment to be retained add 另一点是当您希望保留第一个片段时添加

setRetainInstance(true);

in onCreate of fragment. 在片段的onCreate中。

Please find this demo at GitHub inpulse-fragment 请在GitHub inpulse-fragment上找到此演示

In this demo at first fragment when you will increase the counter it'll appear in text view above and if you click on that text view you will be navigate to Fragment B. Now you can try rotating. 在此演示的第一个片段中,当您增加计数器时,它将显示在上方的文本视图中,如果您单击该文本视图,将导航至片段B。现在您可以尝试旋转。 Let me know in case of any query. 如有任何查询,请通知我。

您可以在onAttach使用setRetaininstance(true) ,并在活动清单文件中使用ConfigChanges = "screenSize|keyBoardHidden|orientation"这行。

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

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