How can I switch between two fragments, without recreating the fragments each time?

后端 未结 10 1887
孤独总比滥情好
孤独总比滥情好 2020-12-08 00:19

I\'m working on an android application, that uses a navigation drawer to switch between two fragments. However, each time I switch, the fragment is completely recreated.

10条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 01:14

    I did this before like this:

            if (mPrevFrag != fragment) {
    
                // Change
                FragmentTransaction ft = fragmentManager.beginTransaction();
                if (mPrevFrag != null){
                    ft.hide(mPrevFrag);
                }
                ft.show(fragment);
                ft.commit();
                mPrevFrag = fragment;
    
            }
    

    (you will need to track your pervious fragment in this solution)

提交回复
热议问题