FragmentManager popBackStack不会删除片段 [英] FragmentManager popBackStack doesn't remove fragment

查看:155
本文介绍了FragmentManager popBackStack不会删除片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施使用片段菜单导航。于是,我开始与民政,然后用户就可以浏览到diferent每个部分的部分和细节。

I'm implementing menu navigation using Fragments. So I begin with Home, and then users can navigate to diferent sections and details of each section.

当用户更改部分,然后我打电话流行的fragmentmanager backstack直到我到家,然后加载新的部分。

When a user changes section, then I call pop on the fragmentmanager backstack until I reach Home, and then load the new section.

这是一切按预期工作。但我发现了这个问题:

This is all working as expected. But I'm getting this problem:

  1. 在加载部分调用setHasOptionsMenu(真)在onResume()
  2. 在加载另一节(老款它suposed让出栈)。我看到它确定。不显示菜单时
  3. 将应用程序(例如,去到Android Laucher活动),然后当我回来,我看到正确的部分,但它显示了旧片段的菜单。

我已经重复了backstack和印刷各片段,而且它没有与菜单中的片段。

I've iterated the backstack and printed each fragment, and there it's not the fragment with the menu.

我把调试标志在onResume()方法(其中 setHasOptionsMenu(真)被标记),它确实进入这里,所以片段,它仍然在某处。

I put a debug mark on the onResume() method (where the setHasOptionsMenu(true) is flagged) and it indeed enters here, so the Fragment it's still somewhere.

我想知道如果我做错事,我怎么能解决这个问题,THX

I want to know if I'm doing something wrong and how could I solve it, thx

更新:

我用这个code装载新片段

I'm using this code to load new fragments

fm.beginTransaction()
    .add(container, sectionFragment.getFragment())
    .addToBackStack(sectionFragment.getFragmentName())
    .commit();

而对于删除:

And for remove:

private void clearStack(){
int count = fm.getBackStackEntryCount();
    while(count > 1){
        fm.popBackStack();
        count--;
    }
}

注1:我使用的是加,而不是替代,因为我不想失去我的片段的状态,当我浏览的节回来。当我打开另外一个不同的部分,然后我打电话clearStack弹出堆栈到1,然后加载新的片段。最后,我打电话executePendingTransactions()来完成从事务中删除片段。

NOTE 1: I'm using add instead replace because I don't want to loose the state of my fragment when I navigate back from detail section. When I load another different section, then I call clearStack to pop the stack up to 1, and then loads new fragment. At the end, I'm calling executePendingTransactions() to finish to remove the fragments from the transaction.

注2:我看到它正在进入我的片段的onDestroy()方法,所以它suposed被破坏。但我不知道为什么它是越来越再次拨通了主要活动恢复的时候。

NOTE 2: I'm seeing that it is entering on my fragment onDestroy() method, so it is suposed to be destroyed. But I don't know why it is getting called again when the Main activity resumes.

推荐答案

我发现这个问题是不是在添加和删除堆栈段的逻辑。

I found that the problem was not in the logic of adding and removing fragment of the stack.

的问题是,某些片段的加载在其内部的另一片段(它有ViewPager成分)。然后,我认为删除的片段,然后当这些片段被删除了。

The problem was that some of the fragment loaded another fragments inside of it (it had ViewPager component). Then I thought that when the fragment was removed then these fragments were removed too.

只有在使用 getChildFragmentManager()方法,这是真实的。里面装其他fragmets片段时,此方法必须使用。如果不是,则该片段asociated与片段的活性。

This is true ONLY if you use getChildFragmentManager() method. This method MUST be used when loading fragments inside other fragmets. If not, then the fragments are asociated with the fragments activity.

这篇关于FragmentManager popBackStack不会删除片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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