Android的 - 在backStack片段的数量限制? [英] Android - limit number of fragments in backStack?

查看:281
本文介绍了Android的 - 在backStack片段的数量限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有一个活动,和片段被添加到它(搜索,歌曲详细信息,设置等)。我实现了基于边的菜单导航,所以现在,作为一个副作用,tehre没有限制多少片段被添加到Backstack。 有没有什么办法可以限制碎片的数量,或删除旧条目?例如每首歌曲的细节片段,有一个推荐的歌曲列表,并通过你可以去到另一首歌曲的细节片段。这是很容易可以有30片在backstack,而如果你有DDMS打开,就可以看到堆大小慢(但肯定)增加。

Currently I have one activity, and fragments are being added to it (search, song details, settings, etc). I implemented side based menu navigation, so now, as a side effect, tehre's no limit to how many Fragments get added to the Backstack. Is there any way I can limit the number of fragments, or remove older entries? Each song details fragment for instance, has a recommended song list, and through that you can go to another song details fragment. It's easily possible to have 30 fragments in the backstack, which if you have DDMS open, you can see the heap size slowly (but surely) increasing.

编辑:有一件事我没有尝试做的是,如果用户点击的侧面菜单选项之一,如果该片段已在b​​ackstack试图回到那个片段,而不是实例化一个新的,但当然,如果用户是乐曲的详细信息页面上,然后他会期望pressing回来会带他到片段,这样是行不通的。

One thing I did try to do is if a User clicked one of the side menu options, if that fragment is already in the backstack try to go back to that fragment instead of instantiating a new one, but of course, if a user is on a Song Details page, then he would expect pressing back would take him to that Fragment so that won't work.

编辑2:
这是我的addFragment方法(与菲尔的建议一起):

Edit 2: This is my addFragment method (along with Phil's suggestion):

public void addFragment(Fragment fragment) { 

        FragmentManager fm = getSupportFragmentManager();
        if(fm.getBackStackEntryCount() > 2) {

              fm.popBackStack();
            }
        fm.beginTransaction()                 
                  .replace(R.id.fragment_container, fragment).addToBackStack("")
                  .commit();
}

我刚刚试了一下,假设我的碎片历史:A-> B-> C-> D,由D回去,去B-> A->退出

I just tried it, and assuming my Fragment history is: A->B->C->D, going back from D, goes B->A->exit.

我刚刚去了8级深测试:A-> B-> C-> D-> E-> F-> G-> H,并打算自H回来,同样的事情发生了:H-> B- > A->退出。

I just went 8 levels deep to test: A->B->C->D->E->F->G->H, and going back from H, same thing happened: H->B->A->exit.

所有碎片,得到通过该方法添加的上方。我想看到的是:H-> G-> F->退出

All Fragments are getting added through that method above. What I would like to see is: H->G->F->exit.

推荐答案

您可以通过编程控制你BackStack碎片的数量:

You can programatically control the number of Fragments in your BackStack:

FragmentManager fm = getActivity().getSupportFragmentManager();

if(fm.getBackStackEntryCount() > 10) {

   fm.popBackStack(); // remove one (you can also remove more)
}

只要检查有多少个片段中有你的Backstack,如果有一个溢出删除。

Simply check how many Fragments there are in your Backstack and remove if there is an "overflow".

如果您要删除从BackStack特异片段,你将不得不实现自己的BackStack 并覆盖onBack pressed()。由于该片段BackStack是栈(顾名思义)时,只有顶部元件(最后添加)可以去掉,不存在在之间除去碎片的可能性。

If you want to remove specific Fragments from the BackStack, you will have to implement your own BackStack and override onBackPressed(). Since the Fragment BackStack is a Stack (as the name indicates), only the top element (the last added) can be removed, there is no possibility of removing Fragments in between.

例如,你可以使用

ArrayList<Fragment>

要实现你自己的堆栈。 简单地添加和删除从堆栈片段(它不是一个真正的协议栈了),只要你的愿望,并通过覆盖pssed()方法onBack $ P $处理previous片段的加载。

to realize your own stack. Simply add and remove Fragments from that "stack" (it's not really a stack anymore) whenever you desire and handle the loading of previous fragments by overriding the onBackPressed() method.

这篇关于Android的 - 在backStack片段的数量限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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