-1

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(...)
user2331454
  • 345
  • 1
  • 3
  • 14
  • Why not update home button as soon as you add fragment to backstack? I mean, you probably already have some method that does it, it might as well update button *and* add fragment to view – wasyl Jun 06 '15 at 19:01

1 Answers1

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

Community
  • 1
  • 1
Elltz
  • 10,730
  • 4
  • 31
  • 59
  • Thanks for the "indirect post", fragmentManager.addOnBackStackChangedListener() was the solution for me. – user2331454 Jun 07 '15 at 11:10
  • okay sir, well then upvote that `indirect post` since its helpful, and if you feel accept this,as it may also help someone to follow up-anyways glad the problem is gone @user2331454 – Elltz Jun 07 '15 at 13:51