Android show back button depending on backstack count

不想你离开。 提交于 2020-01-26 04:27:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!