Android:无法了解popBackStackImmediate()的行为 [英] Android: Not getting how popBackStackImmediate () behaves

查看:815
本文介绍了Android:无法了解popBackStackImmediate()的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个片段,流程就像

I have 3 Fragments and the flow is like

片段1->片段2(将其添加到堆栈中)->片段3->片段1

Fragment 1 --> Fragment 2 (adding it to back stack)--> Fragment 3 --> Fragment 1

当我想再次从Fragment 3转到Fragment 1时,我希望后台堆栈是干净的,以便当用户向后按时没有任何反应.

As I want to go to Fragment 1 from Fragment 3 again, I want the back stack to be clean so that when user press back nothing happens.

最初,我已经清除了片段3的弹出式堆栈

Intially I have cleared popback stack at Fragment 3

Boolean isPoped = fManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

然后创建我的片段1的新实例

And then created a new Instance of my fragment 1

HomeFragment homeFragment = HomeFragment.newInstance(mobileNumber, "");
FragmentTransaction transaction =fManager.beginTransaction();
transaction.replace(R.id.fragment_container, homeFragment);
transaction.commit();

这引起了 android:ViewPager抛出空指针

然后我注释了上面的代码,只使用了

then I commented the above code and only used

Boolean isPoped = fManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

在删除交易(我相信)并将其带到homeFragment时工作正常,但导致

which works fine as it removed the transactions(that I belive) and took me to homeFragment but that caused View pager not instantiating post fragment popBackStackImmediate

在这里,我不了解popBackStack的工作原理吗?我想要做的就是将用户从Fragment 3转到Fragment 1并清除Backstack.

Here I am not getting how popBackStack works? All I want is to take user from Fragment 3 to Fragment 1 and clear the backstack.

推荐答案

我在另一个问题中查看了您的代码,我想我明白了问题所在.

I looked at your code in the other question and I think I understand what the problem is.

因此,您的第一个动作是创建片段1并将其放入容器中.我没有看到相应的代码,但是没关系.如果容器为空,则addreplace的行为相同.因此,现在您的片段1在容器中.

So your first action is that you create Fragment 1 and put it in a container. I don't see code for that but no matter; if the container is empty then add and replace act the same way. So now your Fragment 1 is in the container.

您在这里没有调用addToBackStack,因此FragmentManager不知道如何撤消此操作.很好,这是第一个操作,不需要撤消.

You haven't called addToBackStack here so the FragmentManager doesn't know how to undo this operation. That's fine, it's the first operation and doesn't need to be undone.

接下来,您将创建片段2并执行replace事务.片段1替换为片段2.将片段1压入堆栈,以便FragmentManager 确实知道如何撤消该事务.

Next you create Fragment 2 and do a replace transaction. Fragment 1 is replaced with Fragment 2. This is pushed to the back stack so the FragmentManager does know how to undo this transaction.

接下来,您将创建片段3并执行replace事务.片段2替换为片段3.您无需将此操作推送到后堆栈.哦.

Next you create Fragment 3 and do a replace transaction. Fragment 2 is replaced with Fragment 3. You don't push this operation to the back stack. Uh oh.

现在,当您说popBackStack(延迟或立即执行)时,FragmentManager会撤消将Fragment 2替换为Fragment 1 的操作,但Fragment 2不再在容器中.因此(据我了解),FragmentManager会将片段1放回容器中,但将片段3留在容器中.它不知道如何处理片段3.

Now when you say popBackStack (deferred or immediate) the FragmentManager goes to undo the operation to replace Fragment 2 with Fragment 1 but Fragment 2 isn't in the container anymore. So (from what I understand) the FragmentManager will put Fragment 1 back in the container but leave Fragment 3 there. It doesn't know what to do with Fragment 3.

这是我处理这类情况的方式:假设我有片段1,然后导航至片段2.要在我打算将后退按钮转到片段1时导航至片段3,请执行以下操作:

Here is how I handle those kinds of scenarios: Say I have Fragment 1, then navigate to Fragment 2. To navigate to Fragment 3 when I intend the back button to go to Fragment 1, I do this:

  1. 弹出堆栈(立即,现在将片段1放回容器中),然后
  2. 用片段3替换片段1并将该操作推回堆栈.

现在,当我按下后退"按钮时,FragmentManager知道如何撤消我的操作,并将片段1放回片段3所在的位置.这意味着我不必执行任何片段事务即可处理此向后导航,FragmentManager会为我全部处理.

Now when I press the back button, the FragmentManager knows how to undo my operation and puts Fragment 1 back where Fragment 3 was. This means that I don't have to perform any fragment transactions to handle this back navigation, the FragmentManager handles it all for me.

这篇关于Android:无法了解popBackStackImmediate()的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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