2

I created an android application with a navigation drawer. For this I used the android studio template Navigation Drawer Activity. To compine the appbar and navigation drawer it uses the ActionBarDrawerToggle class.

Inside my activity I switch between diffrent fragments. One of them has a hierarchy with more detailed fragments. It's a construct like by this question.

So when you open the first fragment, you can see the drawer toggle. Then you click on a list item, which replaces the first fragment with a more detailed second fragment. After that the up button appears instead of the original drawer toggle. When you click on the up button you come back to the first fragment and the up button changes back to the drawer toggle.

When you open the second fragment again there is no drawer toggle and no up button. When you press the tablet back button, the toggle will appear again but the up button don't show up until you restart the app.

To change the drawer toggle to the up button I use the following commands inside the onCreate of the second fragment:

second fragment onCreate():

//let the drawer toggle disappear
activity.toggle.setDrawerIndicatorEnabled(false); 
//let the up button appear
activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

The functionality of the up button is made with the follwing commands:

activity onCreate():

toggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
});

activity onBackPressed():

int count = getFragmentManager().getBackStackEntryCount();
if (count < 1) {
     super.onBackPressed();
     Intent intent = new Intent(Intent.ACTION_MAIN);
     intent.addCategory(Intent.CATEGORY_HOME);
     startActivity(intent);
} else {
     getFragmentManager().popBackStack();
     if (count == 1) {
          toggle.setDrawerIndicatorEnabled(true);
     }
}

Opening the second fragment I made with this code inside the first fragment:

first fragment ownOpenMethod():

activity.getFragmentManager().beginTransaction()
     .replace(R.id.content_frame, FirstFragment.newInstance())
     .addToBackStack(null).commit();

The user Wolfram Rittmeyer reported this problem here:

When using a toolbar I had to switch the display options to not use the home as up in the meantime. Otherwise the setDisplayOptions() method within the ToolbarWidgetWrapper (of the internal android.support.v7.internal.widget package) wouldn't recreate the icon when entering the same fragment a second time. Just leaving this here for when others stumble upon this problem as well.

But I don't understand what he means. Had somebody the sameproblem or any example code how to fix?

Community
  • 1
  • 1
xeroxon
  • 21
  • 3

1 Answers1

3

I figured it out! It means that in

@Override
public void onBackPressed() {
...
}

you need to include this:

getSupportActionBar().setDisplayHomeAsUpEnabled(false);

So in the second time, your back icon will appear.