0

I have MainActivity extends AppCompatActivity. In that 3 fragments which can be changed by sliding gesture. I want to remove action bar in middle one. And Add action bar again when user slide in 1st or 3rd fragment.

I have below code:

@Override
public void setMenuVisibility(boolean menuVisible) {
    super.setMenuVisibility(menuVisible);
    if(menuVisible && getActivity()!=null){
        getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}

@Override
public void setMenuVisibility(boolean menuVisible) {
    super.setMenuVisibility(menuVisible);
    if(menuVisible){
        getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
}

But, it's giving me below warning:

Skipped 45 frames! The application may be doing too much work on its main thread.

I tried to put that in:

getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
         // hide / show action bar...
    }
});

App is lagging. How can I make it smooth?

RNK
  • 5,582
  • 11
  • 65
  • 133
  • Are you using ActionBar or can you go for a toolbar? – Pato94 Jun 29 '16 at 19:16
  • Can you consider use `getSupportActionBar().show()`/`getSupportActionBar().hide()`? – betorcs Jun 29 '16 at 19:19
  • @betorcs: There is no such a method in my case. I also tried with `getActivity().getActionBar().show();`. But, in that case also app is crashing: `Attempt to invoke virtual method 'void android.app.ActionBar.show()' on a null object reference` – RNK Jun 29 '16 at 19:21
  • You need cast `getActivity()` to `AppCompatActivity`, so `((AppCompatActivity) getActivity()).getSupportActionBar()` – betorcs Jun 29 '16 at 19:25
  • @betorcs: Crashing: `Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.show()' on a null object reference` – RNK Jun 29 '16 at 19:32
  • Maybe you calling it, before fragment be attach to activity. – betorcs Jun 29 '16 at 19:36

1 Answers1

1

Add the following code snippet in the onResume callback of the middle Fragment

AppCompatActivity activity = (AppCompatActivity) getActivity();
if (activity != null) {
    activity.getSupportActionBar().hide();
}

and in the onResume callback of the other two Fragments add

AppCompatActivity activity = (AppCompatActivity) getActivity();
if (activity != null) {
    activity.getSupportActionBar().show();
}
klutt
  • 30,332
  • 17
  • 55
  • 95
Sakchham
  • 1,689
  • 12
  • 22