Update selected state of navigation drawer after back press

后端 未结 6 1156
臣服心动
臣服心动 2021-02-19 22:56

Whats the proper way to handle the selected state of the navigation drawer after back press?

I have a navigation drawer with n entries (in a listview) like the SDK sampl

相关标签:
6条回答
  • 2021-02-19 23:36

    This pattern is described in the Implement Back Navigation for Fragments section of the "Proper Back Navigation" documentation.

    If your application updates other user interface elements to reflect the current state of your fragments, such as the action bar, remember to update the UI when you commit the transaction. You should update your user interface after the back stack changes in addition to when you commit the transaction. You can listen for when a FragmentTransaction is reverted by setting up a FragmentManager.OnBackStackChangedListener:

    getSupportFragmentManager().addOnBackStackChangedListener(
        new FragmentManager.OnBackStackChangedListener() {
            public void onBackStackChanged() {
                // Update your UI here.
            }
        });
    

    That would be the proper place to refresh the current option in the navigation drawer.

    0 讨论(0)
  • 2021-02-19 23:44

    today I had the same strugle. I solved it by implementing an Interface FragmentToActivity in every Fragment and in my one and only MainActivity.

    Interface:

    public interface FragmentToActivity {
        public void SetNavState(int id);
    }
    

    Fragments:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ((FragmentToActivity) getActivity()).SetNavState(R.id.nav_myitemid); //just add this line
    }
    

    MainActivity:

    @Override
    public void SetNavState(int id) {
        NavigationView nav = (NavigationView) findViewById(R.id.nav_view);
        nav.setCheckedItem(id);
    }
    

    And if you have no idea, where R.id.nav_myitemid comes from, here is my activity_main_drawer.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android">    
        <group android:checkableBehavior="single">
            <item
                android:id="@+id/nav_myitemid"
                android:title="@string/home" />
        </group>
    </menu>
    
    0 讨论(0)
  • 2021-02-19 23:49

    This is late but for those who are using navigation menu instead of list, here is how i did it

    @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            // Handle navigation view item clicks here.
           int position=0;
           Fragment fragment;
            int id = item.getItemId();
            if (id == R.id.nav_home) {
                fragment=new HomeFragment();
                position=0;
    
            } else if (id == R.id.nav_profile) {
    
               fragment=new ProfileFragment();
                position=1;
            }  else if (id == R.id.nav_settings) {
                 fragment=new SettingsFragment();
                position=2;
            }
    
              getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.relative_container, fragment)
                .addToBackStack(""+position)
                .commit();
    
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    

    and add listner for backstack

     getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
    
                 FragmentManager fm = getSupportFragmentManager();
                 String name = fm.getBackStackEntryAt(fm.getBackStackEntryCount()-1).getName();
                   if(!TextUtils.isEmpty(name))
                 selectNavigationDrawerItem(Integer.parseInt(name));
    
            }
        });
    

    selectNavigationDrawerItem method

     private void selectNavigationDrawerItem(int position){
    
                   if(position==0){
                       navigationView.setCheckedItem(R.id.nav_home);
                   }
                   else if(position==1){
                       navigationView.setCheckedItem(R.id.nav_profile);
                   }
                   else if(position==2){
                       navigationView.setCheckedItem(R.id.nav_settings);   
                  }
    
         }
    
    0 讨论(0)
  • 2021-02-19 23:57

    I have had alot of trouble trying to figure out a solution to this problem and the solution I ended up using isn't "nice" in my opinion, but it works.

    In my application i ended up adding a fragments position in the navigation drawer, when i added the fragment to the backstack. This way i could call setItemChecked with the position in my onBackStackChanged(). So basicly what i did was this:

    @Override
    public void onBackStackChanged() {
        FragmentManager fm = getFragmentManager();
        String name = fm.getBackStackEntryAt(fm.getBackStackEntryCount()-1).getName();
        mDrawerList.setItemChecked(Integer.parseInt(name),true);
    }
    
    public class DrawerItemClickListener implements android.widget.AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }
    
    private void selectItem(int position) {
        Fragment fragment;
    
        switch (position) {
            case 0: // Home
                fragment = new ContentFragment();
                break;
            case 1: // Another
               fragment = new AnotherFragment();
                break;
            default: // Logout
            {
                finish();
    
                Intent intent = new Intent(this, StartupActivity.class);
                startActivity(intent);
    
                return;
            }
        }
    
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.content_frame, fragment)
                .addToBackStack(Integer.toString(position))
                .commit();
    
        mDrawerList.setItemChecked(position, true);
        setTitle(mNavigationItems[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }
    
    0 讨论(0)
  • 2021-02-19 23:58

    Building upon @matiash 's excellent answer (be sure to +1 his/hers if you +1 my answer)

    To start, we're assuming we're in the Activity

    getFragmentManager().addOnBackStackChangedListener(
        new FragmentManager.OnBackStackChangedListener() {
            public void onBackStackChanged() {
                // Update your UI here.
            }
        });
    

    You can find the current fragment with the following:

    (from https://stackoverflow.com/a/24589081/878159)

    Fragment f = getFragmentManager().findFragmentById(R.id.frag_container);
    if (f instanceof CustomFragmentClass) {
        // do something with f
        f.doSomething();
    }
    

    That should get you a step closer. Then something like this will get you a reference to your nav drawer:

    nav = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.nav);
    

    Then depending how you setup your drawer fragment code:

    nav.updateSelected(position);
    

    The updateSelected(int) method would be in your NavigationDrawerFragment class and might look something like this:

    public void updateSelected(int position) {
        mCurrentSelectedPosition = position;
        if (mDrawerListView != null) {
            mDrawerListView.setItemChecked(position, true);
        }
    }
    

    NOTE: there are many ways to do this, but this should get you started.

    0 讨论(0)
  • 2021-02-20 00:01

    Here is a solution in kotlin.

    In the fragment that you want to be selected and highlighted by the drawer, you simply do:

        override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    
        val navView: NavigationView = activity!!.findViewById(R.id.nav_view)
        navView.setCheckedItem(R.id.name_of_this_fragment)
    
        ...
    }
    
    0 讨论(0)
提交回复
热议问题