popBackStack之后将调用哪种方法? [英] Which method is being called after popBackStack?

查看:304
本文介绍了popBackStack之后将调用哪种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个活动,在其中我称三个片段-每个片段彼此依赖:

A(ctivity)-> f1(片段1,标题{is |应该}:列表)-> f2(片段2,标题{is | should}:概述)-> f3(片段3,标题{is |应该}:概述)应该}:详细信息)

ATM我使用以下方法调用向后跳转:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            FragmentManager fragmentManager = getSupportFragmentManager();
            if (fragmentManager.getBackStackEntryCount()>0){
                fragmentManager.popBackStack();
            }
    }
}

这很好.

我要像这样在每个片段中覆盖ActionBar标题:

ActionBar bar = getSherlockActivity().getSupportActionBar();
bar.setTitle(R.string.title_f3);

在向前导航时(如上图所示),它可以正常工作,但向后导航ActionBar的标题不会更新:

f3(标题{is |应该}:详细信息)-> f2(标题{is}:详细信息,{should}:概述)-> f1(标题{is}:详细信息,{should}:列表)

很明显,只要显示该片段,我就可以再次对其进行更新.但是我的调试器永远不会停止我想要的任何方法,除非将其称为 onResume().

那么在 popBackStack()之后的上一个片段中实际上有任何方法被调用吗?

解决方案

我知道答案来得太晚了,但是对于任何在这里导航的人来说都可以帮上忙!

首先是第一件事:popBackStack()不弹出片段,而是弹出片段事务.因此,最后一个片段事务在被调用时被撤消.如果您当前正在显示FragmentA,而您的交易是:

fragmentTransaction.replace(R.id.your_layout, fragmentB);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

它将用FragmentB替换FragmentA,并将该事务(不是片段)添加到后台堆栈.如果然后单击后退"按钮,它将事务从后堆栈中弹出,这就是用FragmentB替换此FragmentA".从本质上讲,此指令将反转最后一个事务并将其从已执行的事务堆栈中删除.如果原始的FragmentA仍然存在,则使用该FragmentA.如果它被销毁了,它会制造一个新的.

因此,如果尚未销毁该片段,则在popBackStack()上使用后调用该片段,将调用onStart()和onResume()方法.如果Fragment先前已被销毁,那么将从onAttach()开始调用生命周期方法.等同于按活动"上的后退"按钮.

现在很重要的一点是,当我们跳回堆栈时,重新片段生命周期会发生什么?正如片段交易被撤消之前所说的那样:

场景1:您的fragmentB在交易之前不存在. 在这种情况下,在事务期间将调用onCreate()和onAttach()方法,因此如果您调用popBackStack()并反转事务,则片段将被破坏并分离(注意FragmentA可能已经存在,因此替换它不会像我们一样销毁它)不要撤消片段的创建).在这种情况下,将从onAttach()开始调用生命周期方法.

方案2:您的fragmentB在交易之前已经存在.在这种情况下,该片段将不会被破坏,并且下次访问该片段时,将调用onStart()和onResume()方法.

这里的同事介绍了有关使用popbackstack()的一些信息 解决方案

I know this is a bit late for an answer but for anyone who navigates here this might help!

First thing is first: popBackStack()doesn't pop a fragment, it pops a fragment transaction. So the last fragment transaction is reversed upon being called. If you were displaying FragmentA currently and your transaction was:

fragmentTransaction.replace(R.id.your_layout, fragmentB);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

It would replace FragmentA with FragmentB, and add that transaction (not the fragment) to the back stack. If you then hit the back button, it pops the transaction off the back stack, which was "replace this FragmentA with a FragmentB". Essentially, this instruction reverses the last transaction and removes it from the stack of transactions carried out. If the original FragmentA still exists, it uses that one. If it's been destroyed, it makes a new one.

So, if the Fragment hasn't been destroyed, then recalling the fragment after using on popBackStack(), the onStart() and onResume() methods are called. If the Fragment has been destroyed previously, then the lifecycle methods will be called starting from onAttach(). It's the same as pressing the back button on Activities.

Now the important bit, what happens re fragment lifecycle when we pop off back stack? Well as said before the fragment transaction is reversed so:

Scenario 1: Your fragmentB didn't already exist before transaction. In this case the onCreate() and onAttach() methods are called during the transaction so the fragment will be destroyed and detached if you call popBackStack() and reverse the transaction (Note FragmentA probably already existed so replacing it wont destroy it as we're not undoing a fragment creation). In this case the lifecycle methods will be called starting from onAttach().

Scenario 2: Your fragmentB did already exist before transaction. In this case the fragment won't be destroyed and the next time you access it the onStart() and onResume() methods are called.

This fellow here explains a few things about using popbackstack() http://vinsol.com/blog/2014/09/19/transaction-backstack-and-its-management/ and the fragment lifecycle http://vinsol.com/blog/2014/10/22/fragment-view-state-retention-a-dirty-solution/. The other related posts are worth reading too!

这篇关于popBackStack之后将调用哪种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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