当活动正受到破坏后恢复如何从片段删除经理片段 [英] How to remove fragment from fragment manager when activity is restored after being destroyed

查看:188
本文介绍了当活动正受到破坏后恢复如何从片段删除经理片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活性的,其中包含片段F.一个区会显示F通过推到片段经理的背上栈。该片段可能显示一个对话(更具体地,一个DialogFragment)D,也通过将其推到相同的片段管理器的回栈。

I have an activity A which contains fragment F. A shows F by pushing it onto fragment manager's back stack. This fragment may show a dialog(more specifically, a DialogFragment) D, also by pushing it to the same fragment manager's back stack.

我需要能够根据驳回了由F.片段确定通常我会检查,如果D是片段经理的回堆栈,并且使用getFragmentManager()某些情况下对话框ð。popBackStack()将其删除。但是,如果活动被销毁,重建这不工作:

I need to be able to dismiss dialog D under certain circumstances that are determined by fragment F. Normally I would check if D is on the fragment manager's back stack and use getFragmentManager().popBackStack() to remove it. But this doesn't work if the activity gets destroyed and then recreated:

说我将不保留活动标志在Android设置。现在,我的背景的应用程序。活动被破坏,碎片也有同感。现在我再次前台应用程序。 在哪一点片段F和D被添加到片段经理的回栈?这是一个截图我把我穿上的onPostResume断点()方法,我以为是最后一个接在活动周期运行之前,用户可以使用该应用程序:

Say I set "Don't keep activities" flag in Android Settings. Now I background the app. Activity gets destroyed, and the fragments are too. Now I foreground the app again. At what point do the fragments F and D get added to fragment manager's back stack? This is a screenshot I took after I put a breakpoint on A's onPostResume() method, which I assume is the very last one to run in the activity lifecycle, before the user can use the app:

您可能会注意到mAdded字段包含两个元素 - 这些都是恢复片段F和D. 但是他们都没有回堆栈然而,当mBackStack为空

You may notice that mAdded field contains 2 elements - those are the restored fragments F and D. But they are not on the back stack yet, as mBackStack is null!

我希望能够删除D,而Android不会让我这样做,因为它在等待恢复片段pre-现有的状态,它不会将它们添加到后退堆栈,直到一些onPostResume后的时间。

I would like to be able to remove D, but Android won't let me do it, since it's waiting to restore pre-existing state of fragments and it won't add them to the back stack until some time after onPostResume.

因此​​,在本质上,我无法从堆栈中删除片段,因为它不是在堆栈上呢。而且我还可以被添加到堆栈在某些时候,因为,正如你可以从上面的截图,片段经理将其存储在mAdded一个单独的列表,请参阅而且也没有方法,我可以用不prevent它从mAdded删除它。

So in essence, I can't remove the fragment from the stack, since it's not on the stack yet. And I also can't prevent it from being added to the stack at some point, since, as you can see from the screenshot above, fragment manager stores it in a separate list mAdded and there's no methods that I can use to remove it from mAdded.

如何prevent进行恢复已保存的片段?

推荐答案

也许我错了,但据我所知AOS不存储碎片backstack如果活动停止的。它只能恢复最后一个片段显示没有栈上的所有previous片段。结​​果
然而,你可以存储在堆栈和片段陈述自己。只是删除您的对话框中的onPause 通过的onSaveInstanceState 存储一些标志,然后在 onResume 恢复它,或者没有。

Maybe Im wrong but AFAIK AOS doesn't store the fragment backstack at all if activity stops. It could only restore the last shown fragment without all the previous fragments on a stack.
However you could store the stack and fragments state yourself. Just remove your dialog in onPause storing some flag via onSaveInstanceState and then in onResume restore it or don't.

这篇关于当活动正受到破坏后恢复如何从片段删除经理片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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