Android Navigation - Hide action bar for loading/splash screen?

2.3k views Asked by At

I am wondering if there is a good pattern/method for hiding the action bar for splash/loading screens?? I understand the pattern isn't great, but we are retrofitting an existing app that uses patterns like this. Regardless, this would seem like a common thing to want to do for things like dialogs or fullscreen fragments where you might not necessarily want the Action Bar displayed.

I know it can be done fairly simply with something like

 override fun onResume() {
        super.onResume()
        activity?.findViewById<Toolbar>(R.id.toolbar)?.visibility = GONE
    }

    override fun onPause() {
        super.onPause()
        activity?.findViewById<Toolbar>(R.id.toolbar)?.visibility = VISIBLE
    }

But I was just curious if there is a better way to control the Action bar with NavOptions or something similar.

4

There are 4 answers

0
tyczj On

Set the style of the splash screen activity to

Theme.MaterialComponents.DayNight.NoActionBar

Or some similar NoActionBar style

0
Rajnish Sharma On
ActionBar actionBar = getActionBar();
actionBar.hide();

or you can change it directly in Manifest

<activity 
    android:name=".AnActivity"
    android:label="@string/a_string"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen>
</activity>
0
Gabriele Mariotti On

You can use the addOnDestinationChangedListener.
Something like:

navController.addOnDestinationChangedListener { _, destination, _ ->
   if(destination.id == R.id.splash_screen) {
       toolbar.visibility = View.GONE
   } else {
       toolbar.visibility = View.VISIBLE
   }
}

As alternative you can use something different. Don't use a Fragment for the loading screen.
Define an app theme only for the loading screen:

<style name="LoadingTheme" parent="Theme.MaterialComponents.NoActionBar">
    <item name="android:windowBackground">@drawable/loading_background</item>
</style>

In the Manifest apply this theme to your MainActivity:

<activity android:name=".MainActivity"
              android:theme="@style/LoadingTheme">

Finally in your MainActivity:

override fun onCreate(savedInstanceState: Bundle?) {
    setTheme(R.style.AppTheme_NoActionBar)
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    //.....
0
Ajith Ramesh On

1) Inside fragment use,

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

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

2. In Activity use,

getSupportActionBar().hide();

OR by creating a new style in styles.xml

<style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
      <item name="windowActionBar">false</item>
      <item name="windowNoTitle">true</item>
      <item name="android:windowFullscreen">true</item>
</style>

and use the style at the activity of manifest.xml

<activity ...
      android:theme="@style/AppTheme.NoActionBar" > ...
</activity>