Android Navigation Drawer Fragment State

后端 未结 4 1914
北海茫月
北海茫月 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.

    <fragment
        android:id="@+id/selectionFragment"
        android:name="com.example.confessionsrp.SelectionFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    

    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 :)!

    0 讨论(0)
  • 2020-12-14 21:03

    In case someone want's a different approach to this: you could find the fragment on the stack:

    // check if this fragment is on the backstack to avoid creating a new one
        Fragment foundFragment =  fragmentManager.findFragmentByTag("unique_fragment_tag");
        if (foundFragment != null) {
              fragmentManager.popBackStackImmediate("unique_fragment_tag", 0);
                return;
        }
    
    0 讨论(0)
  • 2020-12-14 21:12

    I am using the following code:

    @Override
    public void onNavigationDrawerItemSelected(int position) {
    
        // update the main content by replacing fragments
        FragmentManager fragmentManager = getFragmentManager();
        if(position==0){// selection of tabs content
            fragmentManager
            .beginTransaction()
            .replace(R.id.container,
                    SimulatorFragment.newInstance(position + 1)).commit();
        }else if(position==1){
            fragmentManager
            .beginTransaction()
            .replace(R.id.container,
                    HudFragment.newInstance(position + 1)).commit();
        }else if(position==2){
            // Display the fragment as the main content.
            fragmentManager
            .beginTransaction()
            .replace(R.id.container, 
                    SettingsBasicFragment.newInstance(position +1)).commit();
        }else{
    
        }
    }
    

    You can replace by a new instance the first time and store the fragment, if it is not null, then replace by the stored fragment.

    The activity must implement NavigationDrawerFragment.NavigationDrawerCallbacks

    The fragment constructor and newInstance methods look like this:

    public final class HudFragment extends Fragment {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";
    
    /**
     * Returns a new instance of this fragment for the given section number.
     * @param simulation 
     */
    public static HudFragment newInstance(int sectionNumber) {  
        HudFragment fragment = new HudFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }
    
    public HudFragment() {
    }
    

    To switch fragments by code I use this method inside the NavigationDrawerFragment:

    /**
     * Select a different section
     * @param position
     */
    public void select(int position){
        selectItem(position);
    }
    
    private void selectItem(int position) {
        mCurrentSelectedPosition = position;
        if (mDrawerListView != null) {
            mDrawerListView.setItemChecked(position, true);
        }            
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mFragmentContainerView);
        }
        if (mCallbacks != null) {
            mCallbacks.onNavigationDrawerItemSelected(position);
        }
    }
    
    0 讨论(0)
  • 2020-12-14 21:22

    The second option is to start with the example of navigationDrawer that Android SDK offers. I selected that template of activity when creating the project and almost all the code of my previous answer is produced automatically.

    If you want to keep the fragments after device rotation or similars it is a different thing, you need then to retain the fragments. If not, you just need to save the new instance of the fragment in a variable and check if it is null to create a new one or use the old one.

    0 讨论(0)
提交回复
热议问题