Last active
May 27, 2018 08:11
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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