I am using android navigation components to navigate fragments. I can easily set action bar by using this code in the Main Activity :
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
But If I want to hide the supportActionbar in some of the fragments then what should be the best approach?
If you want to hide Action Bar from the entire application (from all Activities and fragments), then you can use this method. Just go to res -> values -> styles. xml and change the base application to “Theme.
How do we hide the menu on toolbar in one fragment? When you click the show button to open a fragment, you can see the fragment menu items ordered before activity menu items. This is because of the menu item's android:orderInCategory attribute value. When you click the hide button to hide the fragment.
The requestWindowFeature(Window. FEATURE_NO_TITLE) method of Activity must be called to hide the title.
Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();
For the fragments that you want to hide the SupportActionBar
, you can hide it in onResume()
with .hide()
, and show it again in onStop()
with .show()
@Override
public void onResume() {
super.onResume();
ActionBar supportActionBar = ((AppCompatActivity) requireActivity()).getSupportActionBar();
if (supportActionBar != null)
supportActionBar.hide();
}
@Override
public void onStop() {
super.onStop();
ActionBar supportActionBar = ((AppCompatActivity) requireActivity()).getSupportActionBar();
if (supportActionBar != null)
supportActionBar.show();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With