Skip to content

Instantly share code, notes, and snippets.

@VitaliyBelyaev
Last active May 27, 2018 08:11
Show Gist options
  • Save VitaliyBelyaev/df349ba6b44bb255d0d464ca111eac2c to your computer and use it in GitHub Desktop.
Save VitaliyBelyaev/df349ba6b44bb255d0d464ca111eac2c to your computer and use it in GitHub Desktop.
The way to show home as up in fragment when we have items in backstack and hide it when there are not. This snipet should be placed in your MainActivity.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null){
openFragment(MainFragment.newInstance(),false);
}
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
@Override
public void onBackStackChanged() {
boolean showAsUp = getSupportFragmentManager().getBackStackEntryCount()>0;
getSupportActionBar().setDisplayHomeAsUpEnabled(showAsUp);
}
});
}
@Override
public boolean onSupportNavigateUp() {
getSupportFragmentManager().popBackStack();
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment