Switching between Android Navigation Drawer image and Up caret when using fragments

EvilAsh

When using the Navigation Drawer the Android devs are recommending that in the ActionBar "only those screens that are represented in the Navigation Drawer should actually have the Navigation Drawer image" and that "all other screens have the traditional up carat."

See here for details: http://youtu.be/F5COhlbpIbY

I'm using one activity to control multiple levels of fragments and can get the Navigation Drawer image to display and function at all levels.

When creating lower level fragments I can call the ActionBarDrawerToggle setDrawerIndicatorEnabled(false) to hide the Navigation Drawer image and have the Up caret displayed

LowerLevelFragment lowFrag = new LowerLevelFragment();

//disable the toggle menu and show up carat
theDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportFragmentManager().beginTransaction().replace(R.id.frag_layout, 
lowFrag, "lowerFrag").addToBackStack(null).commit();

The problem I'm having is when I navigate back to the top level fragments the Up carat still shows instead of the original Navigation Drawer image. Any suggestions on how to "refresh" the ActionBar on the top level fragments to re-display the Navigation Drawer image?


Solution

Tom's suggestion worked for me. Here’s what I did:

MainActivity

This activity controls all fragments in the app.

When preparing new fragments to replace others, I set the DrawerToggle setDrawerIndicatorEnabled(false) like this:

LowerLevelFragment lowFrag = new LowerLevelFragment();

//disable the toggle menu and show up carat
theDrawerToggle.setDrawerIndicatorEnabled(false);
getSupportFragmentManager().beginTransaction().replace(R.id.frag_layout,   
lowFrag).addToBackStack(null).commit();

Next, in an override of onBackPressed, I reverted the above by setting the DrawerToggle to setDrawerIndicatorEnabled(true) like this:

@Override
public void onBackPressed() {
    super.onBackPressed();
    // turn on the Navigation Drawer image; 
    // this is called in the LowerLevelFragments
    setDrawerIndicatorEnabled(true)
}

In the LowerLevelFragments

In the fragments I modified onCreate and onOptionsItemSelected like this:

In onCreate added setHasOptionsMenu(true) to enable configuring the options menu. Also set setDisplayHomeAsUpEnabled(true) to enable the < in the actionbar:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // needed to indicate that the fragment would 
    // like to add items to the Options Menu        
    setHasOptionsMenu(true);    
    // update the actionbar to show the up carat/affordance 
    getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
}

Then in onOptionsItemSelected whenever the < is pressed it calls the onBackPressed() from the activity to move up one level in the hierarchy and display the Navigation Drawer Image:

@Override
public boolean onOptionsItemSelected(MenuItem item) {   
    // Get item selected and deal with it
    switch (item.getItemId()) {
        case android.R.id.home:
            //called when the up affordance/carat in actionbar is pressed
            getActivity().onBackPressed();
            return true;
        … 
    }
Tom

You have written that, to implement lower-level fragments, you are replacing the existing fragment, as opposed to implementing the lower-level fragment in a new activity.

I would think that you would then have to implement the back functionality manually: when the user pressed back you have code that pops the stack (e.g. in Activity::onBackPressed override). So, wherever you do that, you can reverse the setDrawerIndicatorEnabled.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Switching fragments in navigation drawer

From Dev

Switching fragments in navigation drawer

From Dev

Switching Fragments in navigation drawer using onNavigationItemSelected()

From Dev

Image above Navigation Drawer (using fragments)

From Dev

Short black flashs when switching fragments in navigation drawer

From Dev

How to change fragments using Android navigation drawer

From Dev

Drawer NavigationView + Fragments with viewpager, how to avoid caching data when switching fragments in navigation drawer from one to another?

From Dev

Android - Navigation drawer fragments

From Dev

Android - Navigation drawer fragments

From Dev

Android Navigation Drawer Show Up Indicator for Lower Level Fragments

From Dev

navigation drawer move between fragments

From Dev

Android: Switching between different fragments using tabs

From Dev

Android Navigation Drawer with many fragments

From Dev

Android save Fragments into Navigation Drawer

From Dev

My Navigation bar is not switching between fragments in Android Studio Kotlin

From Dev

Fragments not showing using navigation drawer

From Dev

Android Navigation Drawer set up image from URL

From Dev

Android - How to change fragments in the Navigation Drawer

From Dev

how to avoid Fragments Overlap in android with Navigation Drawer

From Dev

Switch between Fragments with onNavigationItemSelected in new Navigation Drawer Activity template (Android Studio 1.4 onward)

From Dev

Android Tab Navigation between Fragments

From Dev

Android Tab Navigation between Fragments

From Dev

Navigation Drawer Fragments

From Dev

How to hide soft input Keyboard when the Navigation drawer is being toggled , using fragments

From Dev

Will Activity onpause be called when switching between fragments

From Dev

NullPointerException when switching between fragments containing RecyclerView

From Dev

Android kitkat - AppCompat: Doesn't load fragments in my FrameLayout container.(Using a Navigation drawer)

From Dev

How to communicate data between two fragments in a navigation drawer

From Dev

android - Implement UP button action with Navigation Drawer

Related Related

  1. 1

    Switching fragments in navigation drawer

  2. 2

    Switching fragments in navigation drawer

  3. 3

    Switching Fragments in navigation drawer using onNavigationItemSelected()

  4. 4

    Image above Navigation Drawer (using fragments)

  5. 5

    Short black flashs when switching fragments in navigation drawer

  6. 6

    How to change fragments using Android navigation drawer

  7. 7

    Drawer NavigationView + Fragments with viewpager, how to avoid caching data when switching fragments in navigation drawer from one to another?

  8. 8

    Android - Navigation drawer fragments

  9. 9

    Android - Navigation drawer fragments

  10. 10

    Android Navigation Drawer Show Up Indicator for Lower Level Fragments

  11. 11

    navigation drawer move between fragments

  12. 12

    Android: Switching between different fragments using tabs

  13. 13

    Android Navigation Drawer with many fragments

  14. 14

    Android save Fragments into Navigation Drawer

  15. 15

    My Navigation bar is not switching between fragments in Android Studio Kotlin

  16. 16

    Fragments not showing using navigation drawer

  17. 17

    Android Navigation Drawer set up image from URL

  18. 18

    Android - How to change fragments in the Navigation Drawer

  19. 19

    how to avoid Fragments Overlap in android with Navigation Drawer

  20. 20

    Switch between Fragments with onNavigationItemSelected in new Navigation Drawer Activity template (Android Studio 1.4 onward)

  21. 21

    Android Tab Navigation between Fragments

  22. 22

    Android Tab Navigation between Fragments

  23. 23

    Navigation Drawer Fragments

  24. 24

    How to hide soft input Keyboard when the Navigation drawer is being toggled , using fragments

  25. 25

    Will Activity onpause be called when switching between fragments

  26. 26

    NullPointerException when switching between fragments containing RecyclerView

  27. 27

    Android kitkat - AppCompat: Doesn't load fragments in my FrameLayout container.(Using a Navigation drawer)

  28. 28

    How to communicate data between two fragments in a navigation drawer

  29. 29

    android - Implement UP button action with Navigation Drawer

HotTag

Archive