I would like to implement Home/Up button in the actionBar
. I have simple application with one Activity (A) and two fragments (Settings (B) and About (C)).
In the other questions I have found to use popBackStack
, but that's not the solution bacause if user goes A > B > C > B > C, back-button goes C > B > C > B > A (is this correct behaviour?).
switch (item.getItemId()){
case R.id.action_settings:
// get preferences fragment
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new PrefFragment())
.addToBackStack(null)
.commit();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
return true;
case R.id.action_about:
// get about fragment
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new AboutFragment())
.addToBackStack(null)
.commit();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
return true;
case android.R.id.home:
// clear back stack and show home screen?
getSupportActionBar().setDisplayHomeAsUpEnabled(false);
return true;
}
I would need last case to hide any fragment (show main activity window) and clear my back stack - go home, not back. How could I achieve that?
I use AppCompat library.
try this:
or call
Form the documentation POP_BACK_STACK_INCLUSIVE is