问题
I want to show or hide the back button of the action bar depending on the current backstack entry count. That means I want to show it when there is a fragment (or more) in the backstack, and hide when backstack is empty
getSupportActionBar().setDisplayHomeAsUpEnabled(b);
The problem is, when I start a new fragment and ask for the current backstack entry count, it's still the same like before. It refreshes after some milliseconds.
Also in the started fragment's onResume method, the replaced fragment still can not be found in backstack, backstack count is still the same.
Currently I have an AsyncTask that refreshes the button after 50 milliseconds. Working, but this can't be a good solution.
Any ideas?
Best regards
Edit:
Solution was
fragmentManager.addOnBackStackChangedListener(...)
回答1:
There is no need for AsyncTask
you already have a Backstack
interface that triggers when a Fragment is being added or removed onBackStackChangeListener
so instead of your call implement that and call getBackstackEntryCount()
if its not 0 then you know what you will do
hen I start a new fragment and ask for the current backstack entry count, it's still the same like before
you need to add your transaction to the Backstack
after your add()
or replace()
calls on your FragmentTransaction
like this .addToBackstack("the identifier")
some indirect post
Hope it helps
来源:https://stackoverflow.com/questions/30686186/android-show-back-button-depending-on-backstack-count