Renato Probst
Renato Probst

Reputation: 6054

How to proper disable action bar setDisplayHomeAsUpEnabled for main fragment when using fragment layouts on android?

Hi, im implementing an android application with the fragments concept. Im using Sherlock action bar library. I have a mainActivity and 5 fragments, when the activity loads, if there is no session saved, it loads the first fragment (main fragment).

Im using this piece of code in every fragment i need the setDisplayAsHome enabled:

    getSherlockActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(true);

And this on the MainFragment:

    getSherlockActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(false);

If i wont have a main fragment, i would be using the first piece of code direct on the MainActivity , but this means that even the mainFragment would have a back button showed, and thats the problem.

So how can i disable setDisplayHomeAsUpEnabled(true) only in my main fragment (and setting false only on it wont work, it stays false and i have to add true in the others fragments)?

Upvotes: 0

Views: 776

Answers (1)

Atul O Holic
Atul O Holic

Reputation: 6792

ActionBar belongs to the Activity and not the Fragment, so its only one ActionBar that is displayed through-out all of your Fragment on the top.

Hence if you are hiding it or any of its components in one fragment, you are actually hiding the only ActionBar of your Activity (the same Activity for all the fragments), it will stay hidden till you display it again. This is how it is suppose to work.

For your case you will have to enable it for rest of the Fragments apart from the MainFragment. (Yeah you were correct)

To ease your self you can create two methods in your Activity to show and hide like,

public void showHome(){
   getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

and,

public void hideHome(){
   getSupportActionBar().setDisplayHomeAsUpEnabled(false);
}

and then call it in respective Fragments as,

getSherlockActivity().showHome() n viceversa.

Not much of a help but will still save some efforts. :)

Upvotes: 3

Related Questions