📜 ⬆️ ⬇️

Navigation Drawer + Fragments: Finish the Google Guide

Take an example on the link - it works fine. Add one ma-a-scarlet line:

fragmentTransaction. addToBackStack(null); 

and it immediately turns out that the title in the ActionBar is not updated when returning, nor is the position in the Navigation Drawer itself ... It would seem that you can assign the header to update the header itself, but, first, this is not trivial, since there is no direct access to getSupportActionBar () from FragmentActivity, and, secondly, you need to somehow inform the Navigation Drawer that a completely different item of the list should be highlighted. Which one then?


')
My solution is below.

To get access to the native ActionBar and Navigation Drawer from the fragment, it is more logical to use the listener to connect with the Activity that contains the fragment. This solution, by the way, and Google itself offers for such interaction .

Everything is great, the title in the Activity is easy to pass, but how can the fragment tell the Navigation Drawer which particular list item to select? Send the number of his position, about which he, like, knows from somewhere?

Let's complicate the task - there are several Activities that can use the same fragments, moreover, in different positions of the Navigation Drawer. There is no direct sending of the position number from the fragment. And well, for this is a crutch, as opposed to sending a header, which is a crutch.

In general, we write somewhere constants with the types of all fragments (I already had them in the fragment factory):

 public static final int FRAGMENT_MAIN_CALENDAR = 1; public static final int FRAGMENT_MAIN_CATALOG = 2; public static final int FRAGMENT_MAIN_FAVORITES = 3; public static final int FRAGMENT_AUTHOR_ABOUT = 4; public static final int FRAGMENT_AUTHOR_STORIES = 5; 

and pass them on to the Activity, which itself will figure out what position the Navigation Drawer is currently assigned to.

And I still had to send the header, but not to generate it in the Activity, since Sometimes it is tied to the fragment's content (for example, the author’s full name or the name of an open story), but I don’t want to generate an extra query to the database.
In general, here is the interface:

 public interface OnFragmentChangedListener { public void onFragmentChanged(String title, int fragmentType); } 

Its implementation and implements the method:

 @Override public void onFragmentChanged(String title, int fragmentType) { switch (fragmentType) { case FragmentsFabrica.FRAGMENT_MAIN_CALENDAR : mDrawerList.setItemChecked(0, true); break; case FragmentsFabrica.FRAGMENT_MAIN_CATALOG : mDrawerList.setItemChecked(1, true); break; case FragmentsFabrica.FRAGMENT_MAIN_FAVORITES : mDrawerList.setItemChecked(2, true); break; } setTitle(title); } 

where setTitle is from a google example.
The fragments in onAttach register the Activity as a listener and call it in onCreateView or in onActivityCreated with its fragment, individual header and type:

 onFragmentChangedListener.onFragmentChanged( getResources().getString(R.string.navigation_main_section1_title), FragmentsFabrica.FRAGMENT_MAIN_CALENDAR); 

But I did not manage to solve one problem. For some reason, when you cast the fragments back to the end, the Activity shows a blank screen for a snack and closes only by another pressing of the Back button. At the same time, on this empty screen, a stack of zero-length fragments - would you not close with a fig? I had to add a crutch to the Activity:

 @Override public void onBackPressed() { super.onBackPressed(); FragmentManager fm = getSupportFragmentManager(); if (fm.getBackStackEntryCount() == 0) this.finish(); } 

With what it is connected, and how to correctly solve the problem is not yet clear.

Source: https://habr.com/ru/post/257383/


All Articles