1

I have three classes that extends Fragment (Not FragmentActivity).I want to hide the status bar on second and third fragment..How do i do that?

Note: all three fragment is hosted by same activity.If I hide the actionBar for second fragment It hides the actionbar for all three.

Muhidul Hassan
  • 359
  • 2
  • 4
  • 17

2 Answers2

1

You can override the onTabSelected to show/hide -

 @Override
public void onTabSelected(ActionBar.Tab tab,
        FragmentTransaction fragmentTransaction) {

    //get the tab position and show/hide 
    if (tab.getPosition() == 0) {
        ActionBar actionBar = getActionBar();
        actionBar.hide();
    } else {
        ActionBar actionBar = getActionBar();
        actionBar.show();
    }
}

UPDATE:

On onResume() method of each fragment you want to show your action bar -

@Override
public void onResume() {
    super.onResume();
    ActionBar actionBar = getActionBar();
    actionBar.show();
}

On onResume() method of each fragment you want to hide your action bar -

@Override
public void onResume() {
    super.onResume();
    ActionBar actionBar = getActionBar();
    actionBar.hide();
}
sjain
  • 23,126
  • 28
  • 107
  • 185
  • Did you tried knowing the position of each tab. Determine position and substitute in place for the above code. It will work because each tab has its own action bar and that you can show/hide. The `onTabSelected` method will give you the selected tab and its `getPosition()` method will give the tab position. Match them and show/hide. – sjain Jul 07 '14 at 13:26
  • I dont have PageTitleStrip to show the tab name.I am using no title for tabs...User can see the pages just swipping to the right...There is no name of the tabs...I guess this is why this method not working.Am i right? – Muhidul Hassan Jul 07 '14 at 13:40
  • Its working but I think this is not efficient..May be I need to switch to Fragment Activity..Can you tell me if my class extends fragment activity rather than Fragment will it have swipping option? – Muhidul Hassan Jul 07 '14 at 14:01
  • `I think this is not efficient.` Why you think its not efficient. What are the issues ? `onResume()` is called every time you click and so are you show/hide your action bar. It doesn't matter what class you extend, you still have 3 fragments for one activity. – sjain Jul 07 '14 at 14:07
  • The thing is If I go back from screen 3 to screen 2 it hides the actionBar ..But I need to show actionBar here.. I only want the first screen actionBar to be hidden..But It hides the second screen If I come back from screen 3 to screen 2.But Is i go from screen 1 to screen 3 it works.. – Muhidul Hassan Jul 07 '14 at 14:14
  • `I only want the first screen actionBar to be hidden` Use `actionBar.hide();` on screen 1's onResume(). `But It hides the second screen If I come back from screen 3 to screen 2.` Also use `actionBar.show();` on screen 2's onResume(). – sjain Jul 07 '14 at 14:21
  • As I said, actionBar belongs to an activity. Activity contains 3 fragments. And three fragments have there individual `onResume()` method. You need to write to `onResume()` method of all the 3 fragments and decide what to hide and what to show on each using hide/show methods. For more - refer to http://stackoverflow.com/questions/22415216/how-can-i-hide-the-actionbar-in-a-single-fragment. – sjain Jul 07 '14 at 14:30
0

in oncreate of the fragment add

getSupportActionBar().hide();

or Add getActionBar().hide(); if your are using Support Library.

ygrunin
  • 325
  • 1
  • 4
  • 15
  • Dude .It does not work..It hides actionbar for three fragment..I just want for two and three ..If I write this code in second or third fragment then It hides for first fragment as well. – Muhidul Hassan Jul 07 '14 at 13:18
  • ActionBAr is for Activity, so take the fragments you want to hide to other activity – ygrunin Jul 07 '14 at 13:21