片段栈 [英] Fragment Backstack

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

问题描述

我有一个活动和三个片段.当我替换第一个片段时,我将其添加到了堆栈中.

Hi I have an activity and three fragments. When I replace my first fragment I am adding it to backstack.

FragmentTransaction fragmentTransaction = getFragmentManager()
            .beginTransaction();
fragmentTransaction.replace(R.id.authentication_parent0_linear,
                new LoginFragment(), LOGINTAG);
fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

但是当我在其他两个片段之间移动时,我并没有将它们添加到堆栈中,因此每当我从其他两个片段上按回时,我都需要回到第一个片段.分段或先删除然后再添加第三个,即当我按回时,我将返回第一个分段.但是当我在那两个片段之间导航时,将其删除第二个并加第三个或删除第三个并加第二个等等.在那种情况下,当我按回去时,我退出应用程序,而backstackcount显示为零,但是第一次删除第一个片段时,我将其存储在了backstack中.

But when I move between the other two fragments I am not adding them to backstack.So whenever I press back from the other two fragments I need to go back to first fragment.It works fine when I remove first fragment and add second fragment or remove first and add third i.e when I press back I am coming back to first fragment. But when I navigate between those two fragments that is remove second and add third or remove third and add second etc . At that instance when I press back I am exiting the app and backstackcount is shown as zero but for the first time when I removed the first fragment I stored it in the backstack.

getFragmentManager()
            .beginTransaction()
            .replace(R.id.authentication_parent0_linear,
                    new SignupFragment(), SIGNUPTAG).commit();

所以我的问题是片段后堆栈会在两次提交之间重置.如果是这样,我怎么能第一次在后台获取存储的片段

So my question is will the fragment backstack resets between commits. If so how can I get the stored fragment in the backstack for the first time

推荐答案

实现所需目标的最佳方法是不使用fragment的backstack.

Best way to achieve what you want is not to use fragment's backstack.

  1. 请勿在任何地方使用 addToBackStack()
  2. 在实例字段mLoginFragment中保留您的第一个片段 LoginFragment 参考.第一个片段的添加将如下所示(可能在活动的 onCreate()中:

  1. Do not use addToBackStack() anywhere
  2. Keep your first fragment LoginFragment reference in the instance field mLoginFragment. Addition of your first fragment will look like this (probably in activity's onCreate():

  FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
  mLoginFragment = new LoginFragment();
  fragmentTransaction.replace(R.id.authentication_parent0_linear, mLoginFragment, LOGINTAG);
  fragmentTransaction.commit();

您活动中的

  • 会覆盖 onBackPressed()方法,如下所示:

    @Override
    public void onBackPressed() {
        Fragment fr = getFragmentManager().findFragmentById(R.id.authentication_parent0_linear);
    
        // chek that mLoginFragment!=null, basically this should never happen! 
    
        if (fr == mLoginFragment) {
            super.onBackPressed();
        } else {
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
          fragmentTransaction.replace(R.id.authentication_parent0_linear, mLoginFragment, LOGINTAG);
          fragmentTransaction.commit();
        }
    }
    

  • 有一种繁琐的方法可以通过使用Backstack来获得所需的效果.方法如下:

    There is a cumbersome way to achieve the desired effect with the use of backstack. Here is how:

      创建片段A时
    1. 请勿调用 addToBAckStack()
    2. 从片段A导航到B或C或其他片段时调用 addToBackStack()
    3. 从B到C进行更深的导航时,在事务之前调用 popBackStack().可以在SO上找到它的解释.像这样:

    1. do not call addToBAckStack() when creating fragment A
    2. When navigating from fragment A to B or to C, or to other fragments call addToBackStack()
    3. When navigating deeper from B to C, etc. call popBackStack() before the transaction. Explanation of it can be found on SO. Like this:

    getFragmentManager().popBackStack();
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
      fragmentTransaction.replace(R.id.authentication_parent0_linear, new FragmentC(), "whatever");
      fragmentTransaction.addToBackStack(null);
      fragmentTransaction.commit();
    

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

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