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