安卓onConfigurationChanged:如何保存和恢复片段回栈? [英] Android onConfigurationChanged: how to save and restore fragment back stack?

查看:181
本文介绍了安卓onConfigurationChanged:如何保存和恢复片段回栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动有一个双窗格:在左侧(片段M)菜单项和当前所选项目的详细信息,在正确的(片段D)

名单

当用户选择D片段中的项目,D片段被替换为另一个片段(我们称之为D1)。当用户选择的片段D1项目,片段D1被替换为另一个片段(姑且称之为D2),等等。 当然,当用户presses后退按钮,她可以回去的历史片段:D2 - > D1 - >ð

我的问题是:我怎么能保存和恢复在配置更改的完整片段回栈(特别是在屏幕方向)

这似乎很奇怪,我认为没有人之前有过这个问题,但我在我的谷歌和堆栈溢出的搜索没有发现这事。如果我错过了任何相关的职位,请称呼我吧。

我知道,我可以简单地添加安卓configChanges =方向|屏幕尺寸属性,我的活动,以避免活动的娱乐,但我根本无法做到这一点<。 / P>

我之所以不能做到这一点的是,我使用的操作栏夏洛克(VERS 4)的向后兼容性,但该组件需要活动时的娱乐配置更改正确的行为,AFAIK。

如果有另一种方式来重新操作栏福尔摩斯成分不破坏并重新创建活动,请让我知道。

在此先感谢。

解决方案

我能够通过缓存的片段,因为我加入他们自己的ArrayList中做到这一点。然后,我建立了一个OnBackStackChangedListener跟踪,其中一人表示,并弹出关闭该ArrayList是必要的。

我的目的是有点不同,但低于code应该是什么,你需要什么你描述。该标签,所以你可以有多个回筹码,如果你需要。它不会编译为-是(我裁剪很多我自己的code),但应该给你我是如何做到的的想法。其他免责声明:我只是得到了这个工作,可能有问题,我都打不还。

 公共无效replaceFragmentWithBackStackForTag(片段片段,字符串变量)
   {
      FragmentTransaction英尺= getSupportFragmentManager()的BeginTransaction()。
      ft.detach(visibleFragment);
      ft.add(R.id.realtabcontent,片段,标记);
      ft.attach(片段);
      ft.addToBackStack(空);
      manualBackStacks.get(标签)。新增(片段);
      ft.commit();
      this.getSupportFragmentManager()executePendingTransactions()。
   }
 

c您的$ C $会想,你的活动得到一个方向变化后重建:

 的ArrayList&LT;片断&gt; backStack =
       新的ArrayList&LT;片断&gt;(manualBackStacks.get(标签));
 popArrayListToIndex(manualBackStacks.get(标签),0); //帮助我写
 对于(INT BS = 1; BS&LT; backStack.size(); BS ++){
    replaceFragmentWithBackStackForTag(backStack.get(BS),标签);
 }
 

在backstack监听器:

 公共无效onBackStackChanged(){
    INT指数= getSupportFragmentManager()getBackStackEntryCount()。
    ArrayList的&LT;片断&gt; backStack = manualBackStacks.get(标签);
    visibleFragment = backStack.get(指数);
    //弹出最后一个元素,如果我们备份。
    popArrayListToIndex(backStack,索引);
 }
 

希望这有助于。

I have an activity with a dual pane: a list of menu items on the left (fragment M) and details of the currently selected item on the right (fragment D).

When the user selects an item in fragment D, fragment D gets replaced with another fragment (let's call it D1). When the user selects an item in fragment D1, fragment D1 gets replaced with another fragment (let's call it D2), and so on. Of course, when the user presses the back button she can go back in the fragments history: D2->D1->D.

My problem is: how can I save and restore the full fragment back stack upon configuration change (in particular, upon screen orientation)?

It seems very strange to me that no one else had had this problem before, but I did not find anything about this during my searches on Google and Stack Overflow. If I missed any relevant post, please address me to it.

I know that I could simply add the android:configChanges="orientation|screenSize" attribute to my activity to avoid activity recreation, but I simply cannot do that.

The reason I cannot do that is that I am using Action Bar Sherlock (vers. 4) for backward compatibility and that component needs activity recreation to behave correctly upon configuration change, AFAIK.

If there is another way to recreate the Action Bar Sherlock component without destroying and re-creating the activity please let me know.

Thanks in advance.

解决方案

I was able to do this by caching the Fragments as I added them in my own ArrayList. Then I set up an OnBackStackChangedListener to keep track of which one was shown and pop off the ArrayList as necessary.

My purpose was a little different but the code below should be what you need for what you describe. The tags are so you can have multiple back stacks if you need. It won't compile as-is (I've clipped lots of my own code) but should give you an idea of how I did it. Additional disclaimer: I just got this working and there may be issues I haven't hit yet.

   public void replaceFragmentWithBackStackForTag(Fragment fragment, String tag)
   {
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
      ft.detach(visibleFragment);
      ft.add(R.id.realtabcontent, fragment, tag);
      ft.attach(fragment);
      ft.addToBackStack(null);
      manualBackStacks.get(tag).add(fragment);
      ft.commit();
      this.getSupportFragmentManager().executePendingTransactions();
   }

The code you'll want where your activity gets recreated after an orientation change:

 ArrayList<Fragment> backStack =
       new ArrayList<Fragment>(manualBackStacks.get(tag));
 popArrayListToIndex(manualBackStacks.get(tag), 0); // helper I wrote
 for (int bs = 1; bs < backStack.size(); bs++) {
    replaceFragmentWithBackStackForTag(backStack.get(bs), tag);
 }

The backstack listener:

 public void onBackStackChanged() {
    int index = getSupportFragmentManager().getBackStackEntryCount();
    ArrayList<Fragment> backStack = manualBackStacks.get(tag);
    visibleFragment = backStack.get(index);
    // Pop the last element if we've backed up.
    popArrayListToIndex(backStack, index);
 }

Hope this helps.

这篇关于安卓onConfigurationChanged:如何保存和恢复片段回栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆