首页 文章

添加到后台堆栈时,如何维护片段状态?

提问于
浏览
138

我've written up a dummy activity that switches between two fragments. When you go from FragmentA to FragmentB, FragmentA gets added to the back stack. However, when I return to FragmentA (by pressing back), a totally new FragmentA is created and the state it was in is lost. I get the feeling I' m跟this问题相同,但我已经包含了一个完整的代码示例来帮助解决问题:

public class FooActivity extends Activity {
  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(android.R.id.content, new FragmentA());
    transaction.commit();
  }

  public void nextFragment() {
    final FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(android.R.id.content, new FragmentB());
    transaction.addToBackStack(null);
    transaction.commit();
  }

  public static class FragmentA extends Fragment {
    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
      final View main = inflater.inflate(R.layout.main, container, false);
      main.findViewById(R.id.next_fragment_button).setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
          ((FooActivity) getActivity()).nextFragment();
        }
      });
      return main;
    }

    @Override public void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      // Save some state!
    }
  }

  public static class FragmentB extends Fragment {
    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
      return inflater.inflate(R.layout.b, container, false);
    }
  }
}

添加了一些日志消息:

07-05 14:28:59.722 D/OMG     ( 1260): FooActivity.onCreate
07-05 14:28:59.742 D/OMG     ( 1260): FragmentA.onCreateView
07-05 14:28:59.742 D/OMG     ( 1260): FooActivity.onResume
<Tap Button on FragmentA>
07-05 14:29:12.842 D/OMG     ( 1260): FooActivity.nextFragment
07-05 14:29:12.852 D/OMG     ( 1260): FragmentB.onCreateView
<Tap 'Back'>
07-05 14:29:16.792 D/OMG     ( 1260): FragmentA.onCreateView

它在FragmentA上's never calling FragmentA.onSaveInstanceState and it creates a new FragmentA when you hit back. However, if I' m并且我锁定了屏幕,FragmentA.onSaveInstanceState确实被调用了 . 太奇怪了...我错了预期一个片段被添加到后面的堆栈而不需要重新创建?这是docs所说的:

然而,如果您在删除片段时调用addToBackStack(),则片段将停止,并且如果用户导航回复则将恢复该片段 .

12 回答

  • 77

    如果从后向堆栈返回一个片段,它不会重新创建片段,而是重新使用相同的实例,并在片段生命周期中以 onCreateView() 开头,请参阅Fragment lifecycle .

    因此,如果要存储状态,则应使用实例变量, not 依赖于 onSaveInstanceState() .

  • 0

    与Apple的 UINavigationControllerUIViewController 相比,Google在Android软件架构方面表现不佳 . 关于 Fragment 的Android文档没什么用 .

    从FragmentA输入FragmentB时,不会销毁现有的FragmentA实例 . 当您在FragmentB中按Back并返回FragmentA时,我们将调用't create a new FragmentA instance. The existing FragmentA instance' s onCreateView() .

    The key thing is we should not inflate view again in FragmentA's onCreateView(), because we are using the existing FragmentA's instance. We need to save and reuse the rootView.

    以下代码运行良好 . 它不仅保持片段状态,还减少了RAM和CPU负载(因为我们只在必要时扩充布局) . 我可以't believe Google'的示例代码和文档从不提及它,但always inflate layout .

    Version 1(Don't use version 1. Use version 2)

    public class FragmentA extends Fragment {
        View _rootView;
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            if (_rootView == null) {
                // Inflate the layout for this fragment
                _rootView = inflater.inflate(R.layout.fragment_a, container, false);
                // Find and setup subviews
                _listView = (ListView)_rootView.findViewById(R.id.listView);
                ...
            } else {
                // Do not inflate the layout again.
                // The returned View of onCreateView will be added into the fragment.
                // However it is not allowed to be added twice even if the parent is same.
                // So we must remove _rootView from the existing parent view group
                // (it will be added back).
                ((ViewGroup)_rootView.getParent()).removeView(_rootView);
            }
            return _rootView;
        }
    }
    

    ------ 2005年5月3日更新:-------

    正如所提到的评论,有时 _rootView.getParent()onCreateView 中为空,这会导致崩溃 . 版本2删除onDestroyView()中的_rootView,如dell116建议的那样 . 在Android 4.0.3,4.4.4,5.1.0上测试 .

    Version 2

    public class FragmentA extends Fragment {
        View _rootView;
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            if (_rootView == null) {
                // Inflate the layout for this fragment
                _rootView = inflater.inflate(R.layout.fragment_a, container, false);
                // Find and setup subviews
                _listView = (ListView)_rootView.findViewById(R.id.listView);
                ...
            } else {
                // Do not inflate the layout again.
                // The returned View of onCreateView will be added into the fragment.
                // However it is not allowed to be added twice even if the parent is same.
                // So we must remove _rootView from the existing parent view group
                // in onDestroyView() (it will be added back).
            }
            return _rootView;
        }
    
        @Override
        public void onDestroyView() {
            if (_rootView.getParent() != null) {
                ((ViewGroup)_rootView.getParent()).removeView(_rootView);
            }
            super.onDestroyView();
        }
    }
    

    WARNING!!!

    This is a HACK! Though I am using it in my app, you need to test and read comments carefully.

  • 3

    我想有另一种方法可以实现您的目标 . 我不是说它是一个完整的解决方案,但它符合我的目的 .

    我所做的不是替换片段,而是添加了目标片段 . 所以基本上你将使用 add() 方法而不是 replace() .

    我还做了什么 . 我隐藏了我当前的片段,并将其添加到backstack .

    因此它在当前片段上重叠新片段而不破坏其视图 . (检查它的 onDestroyView() 方法是否被调用.Plus将它添加到 backstate 给了我恢复片段的优势 .

    这是代码:

    Fragment fragment=new DestinationFragment();
    FragmentManager fragmentManager = getFragmentManager();
    android.app.FragmentTransaction ft=fragmentManager.beginTransaction();
    ft.add(R.id.content_frame, fragment);
    ft.hide(SourceFragment.this);
    ft.addToBackStack(SourceFragment.class.getName());
    ft.commit();
    

    如果视图被销毁或未创建,AFAIK系统仅调用 onCreateView() . 但是在这里我们通过不从内存中删除它来保存视图 . 因此它不会创建新视图 .

    当你从Destination Fragment返回时,它将弹出最后一个 FragmentTransaction 删除顶部片段,这将使最顶层(SourceFragment)视图出现在屏幕上 .

    评论:正如我所说,这不是一个完整的解决方案,因为它不会删除源片段的视图,因此占用比平时更多的内存 . 但仍然有用 . 同时我们使用完全不同的隐藏视图而不是替换的机制这是非传统的 .

    所以它不是关于你如何维持状态,而是关于你如何维护视图 .

  • 0

    我在包含 Map 的片段中遇到了这个问题,该 Map 有太多设置细节需要保存/重新加载 . 我的解决方案是基本上保持这个片段一直活跃(类似于@kaushal提到的) .

    假设您有当前的片段A并想要显示片段B.总结后果:

    • replace() - 删除片段A并将其替换为片段B.片段A将再次带到前面重新创建

    • add() - (创建并且)添加片段B并且它与片段A重叠,片段A在后台仍然有效

    • remove() - 可用于删除片段B并返回到A.片段B将在稍后调用时重新创建

    因此,如果您想要将两个片段保存为“已保存”,只需使用hide()/ show()切换它们即可 .

    Pros :保持多个片段运行的简单方法
    Cons :你使用了更多的内存来保持所有内存的运行 . 可能遇到问题,例如显示许多大位图

  • 6

    只有在配置更改时才会调用 onSaveInstanceState() .

    由于从一个片段更改为另一个片段,因此没有配置更改,因此不会调用 onSaveInstanceState() . 什么状态不被保存?你能说明吗?

    如果您在EditText中输入一些文本,它将自动保存 . 任何没有任何ID的UI项是不应保存其视图状态的项 .

  • 0

    这里,因为当你将片段添加到backstack时,片段中的 onSaveInstanceState 不会调用 . 恢复时backstack中的片段生命周期开始 onCreateView 并结束 onDestroyViewonSaveInstanceStateonDestroyViewonDestroy 之间调用 . 我的解决方案是在 onCreate 中创建实例变量和init . 示例代码:

    private boolean isDataLoading = true;
    private ArrayList<String> listData;
    public void onCreate(Bundle savedInstanceState){
         super.onCreate(savedInstanceState);
         isDataLoading = false;
         // init list at once when create fragment
         listData = new ArrayList();
    }
    

    并在 onActivityCreated 中查看:

    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if(isDataLoading){
             fetchData();
        }else{
             //get saved instance variable listData()
        }
    }
    
    private void fetchData(){
         // do fetch data into listData
    }
    
  • 49
    getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener()
        {
            @Override
            public void onBackStackChanged()
            {
                if (getSupportFragmentManager().getBackStackEntryCount() == 0)
                {
                    //setToolbarTitle("Main Activity");
                }
                else
                {
                    Log.e("fragment_replace11111", "replace");
                }
            }
        });
    
    
    YourActivity.java
    @Override
    public void onBackPressed()
    {
     Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.Fragment_content);
      if (fragment instanceof YourFragmentName)
        {
            fragmentReplace(new HomeFragment(),"Home Fragment");
            txt_toolbar_title.setText("Your Fragment");
        }
      else{
         super.onBackPressed();
       }
     }
    
    
    public void fragmentReplace(Fragment fragment, String fragment_name)
    {
        try
        {
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.Fragment_content, fragment, fragment_name);
            fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
            fragmentTransaction.addToBackStack(fragment_name);
            fragmentTransaction.commitAllowingStateLoss();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
  • 0

    我的问题很相似,但是在没有保持片段存活的情况下我克服了我 . 假设您有一个包含2个片段的活动 - F1和F2 . F1最初启动,让我们说包含一些用户信息,然后在某些情况下F2会弹出,要求用户填写其他属性 - 他们的电话号码 . 接下来,您希望该电话号码回弹到F1并完成注册,但您意识到之前的所有用户信息都已丢失,并且您没有以前的数据 . 片段从头开始重新创建,即使您在 onSaveInstanceState 中保存了此信息,该包也会在 onActivityCreated 中返回null .

    Solution: 将所需信息保存为调用活动中的实例变量 . 然后将该实例变量传递到您的片段中 .

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    
        Bundle args = getArguments();
    
        // this will be null the first time F1 is created. 
        // it will be populated once you replace fragment and provide bundle data
        if (args != null) {
            if (args.get("your_info") != null) {
                // do what you want with restored information
            }
        }
    }
    

    接下来我的示例:在显示F2之前,我使用回调将用户数据保存在实例变量中 . 然后我开始F2,用户填写电话号码并按保存 . 我在活动中使用另一个回调,收集这些信息并替换我的片段F1,这次它有我可以使用的包数据 .

    @Override
    public void onPhoneAdded(String phone) {
            //replace fragment
            F1 f1 = new F1 ();
            Bundle args = new Bundle();
            yourInfo.setPhone(phone);
            args.putSerializable("you_info", yourInfo);
            f1.setArguments(args);
    
            getFragmentManager().beginTransaction()
                    .replace(R.id.fragmentContainer, f1).addToBackStack(null).commit();
    
        }
    }
    

    有关回调的更多信息,请访问:https://developer.android.com/training/basics/fragments/communicating.html

  • 0

    首先:只使用add方法而不是FragmentTransaction类的替换方法然后你必须通过addToBackStack方法将secondFragment添加到堆栈第二个:on back click你必须调用popBackStackImmediate()

    Fragment sourceFragment = new SourceFragment ();
    final Fragment secondFragment = new SecondFragment();
    final FragmentTransaction ft = getChildFragmentManager().beginTransaction();
    ft.add(R.id.child_fragment_container, secondFragment );
    ft.hide(sourceFragment );
    ft.addToBackStack(NewsShow.class.getName());
    ft.commit();
    
    ((SecondFragment)secondFragment).backFragmentInstanceClick = new SecondFragment.backFragmentNewsResult()
    {
            @Override
            public void backFragmentNewsResult()
            {                                    
                getChildFragmentManager().popBackStackImmediate();                                
            }
    };
    
  • 0

    使用以下代码替换片段:

    Fragment fragment = new AddPaymentFragment();
    getSupportFragmentManager().beginTransaction().replace(R.id.frame, fragment, "Tag_AddPayment")
                    .addToBackStack("Tag_AddPayment")
                    .commit();
    

    Activity的onBackPressed()是:

    @Override
    public void onBackPressed() {
        android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
        if (fm.getBackStackEntryCount() > 1) {
    
            fm.popBackStack();
        } else {
    
    
            finish();
    
        }
        Log.e("popping BACKSTRACK===> ",""+fm.getBackStackEntryCount());
    
    }
    
  • 114
    Public void replaceFragment(Fragment mFragment, int id, String tag, boolean addToStack) {
            FragmentTransaction mTransaction = getSupportFragmentManager().beginTransaction();
            mTransaction.replace(id, mFragment);
            hideKeyboard();
            if (addToStack) {
                mTransaction.addToBackStack(tag);
            }
            mTransaction.commitAllowingStateLoss();
        }
    replaceFragment(new Splash_Fragment(), R.id.container, null, false);
    
  • 0

    完美的解决方案,在堆栈中找到旧片段并加载它(如果存在于堆栈中) .

    /**
         * replace or add fragment to the container
         *
         * @param fragment pass android.support.v4.app.Fragment
         * @param bundle pass your extra bundle if any
         * @param popBackStack if true it will clear back stack
         * @param findInStack if true it will load old fragment if found
         */
        public void replaceFragment(Fragment fragment, @Nullable Bundle bundle, boolean popBackStack, boolean findInStack) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            String tag = fragment.getClass().getName();
            Fragment parentFragment;
            if (findInStack && fm.findFragmentByTag(tag) != null) {
                parentFragment = fm.findFragmentByTag(tag);
            } else {
                parentFragment = fragment;
            }
            // if user passes the @bundle in not null, then can be added to the fragment
            if (bundle != null)
                parentFragment.setArguments(bundle);
            else parentFragment.setArguments(null);
            // this is for the very first fragment not to be added into the back stack.
            if (popBackStack) {
                fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
            } else {
                ft.addToBackStack(parentFragment.getClass().getName() + "");
            }
            ft.replace(R.id.contenedor_principal, parentFragment, tag);
            ft.commit();
            fm.executePendingTransactions();
        }
    

    用它就像

    Fragment f = new YourFragment();
    replaceFragment(f, null, boolean true, true);
    

相关问题