How to Hide Activity Toolbar From Child Fragment

Imagine if you are working with android navigation drawer which contains more than one child Fragment and the situation is that the parent activity toolbar should be hidden in one of the child fragment but will show in other child fragments, this simple solution will help you.

Part of the code snippets I usually document here are problems I have encounter and how I was able to solve them. Despite the fact I found a working solution I am open to learning from other people who have different solutions or who feel there is need for improvement with my solution.

So if you have a solution on how to hide activity toolbar from child fragment, kindly use the comment box and share your solution.

@Override
public void onResume() {
    super.onResume();
    Objects.requireNonNull(((AppCompatActivity) Objects.requireNonNull(getActivity())).getSupportActionBar()).hide();
}

@Override
public void onStop() {
   super.onStop();
   Objects.requireNonNull(((AppCompatActivity) Objects.requireNonNull(getActivity())).getSupportActionBar()).show();
}

This is basically getting the Activity reference and use the  getSupportActionBar() method to get the activity ActionBar instance. Calling the hide()of the ActionBar instance hides the Toolbar from parent activity.

 

 

Add a Comment