Update selected state of navigation drawer after back press

后端 未结 6 1172
臣服心动
臣服心动 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条回答
  •  萌比男神i
    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:

    
        
        
            
        
    
    

提交回复
热议问题