Android Navigation Drawer Fragment State

后端 未结 4 1965
北海茫月
北海茫月 2020-12-14 20:56

I\'m currently utilizing the Navigation Drawer for my Android APP. In my first fragment, I\'ve a fragment that loads data using Facebook\'s Graph API. Thus, when my App is f

4条回答
  •  死守一世寂寞
    2020-12-14 21:03

    To anyone who encounters the same issue with me,I've managed to find a solution.

    In the container frame,I've to define specific fragment views that I'll be utilizing as shown below.

    Specific fragments

    In each Fragment view,I've to "link" it with the actual Fragment itself as shown below via the "android:name" attribute.Do take note of the the path to the Fragment,example for in my case it's com.example.confesssionsrp.SelectionFragment.

    
    

    In the the MainActivity(or the Activity where we're viewing the fragments),create variables for each of the Fragments as shown below.

    Variables

    Following that,in the Oncreate of the MainActivity(or your specific Activity),initialize the various fragments as shown below.

    OnCreate

    Proceed onto creating a new Method called "ShowFragment" as shown below.

    private void showFragment(int fragmentIndex, boolean addToBackStack) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        for (int i = 0; i < fragments.length; i++) {
            if (i == fragmentIndex) {
                transaction.show(fragments[i]);
                if (Session.getActiveSession().isClosed()) {
                    mDrawerLayout
                            .setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                }
            } else {
                transaction.hide(fragments[i]);
            }
        }
        if (addToBackStack) {
            transaction.addToBackStack(null);
        }
        transaction.commit();
    }
    

    From then on in the switching of the fragments,manually call upon the "ShowFragment" method with the selected Fragment as shown below.

    Show Fragment

    Doing all of this overall,will not reset the Fragment each time we view it,and therefore is the solution to the answer.Thank you for anyone who has helped me so far :)!

提交回复
热议问题