How to display progress screen while restarting app?

Viewed 304

I do app restart with the following intent:

Intent restartIntent = new Intent(context, MainActivity.class);
restartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
restartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(restartIntent);

But there is "white" screen while restarting.

I want to display custom screen instead, while restarting app.

Do you know how to achieve this?

4 Answers

To get rid of the white screen which is because of android's cold start, and appears when the app loads to memory, you can add the following style item to your AppTheme.

 <item name="android:windowDisablePreview">true</item>

Now, In your MainActivity startup, you can display progressBar or anything like placeholder View while your data is being loaded in background and once loaded, hide progress/placeholders etc. You can also create an intermediate Activity/View something like SplashScreen to be displayed on restart Intent instead of using progressBar in your MainActivity.

You probably can't display animating progress but you can create splashscreen. It will style white window which you have while restarting app. Check this and this

I had the same issue and solved it by clearing the stack manually with finishAffinity():

Intent restartIntent = new Intent(context, MainActivity.class);
startActivity(restartIntent);
finishAffinity();

This way, the transition to your MainActivity does not show the blank screen for a short lapse of time. If you want a loading screen, you can go to an Activity with the ProgressBar and use this code in that Activity's onCreate.

I know it is not the best solution but it is the only way I could make it work.

To set a background drawable while an activity is starting you should use something like this:

<style name="AppTheme.NoActionBar.SplashTheme">
    <item name="android:windowBackground">@drawable/splash</item>
</style>

as the activity's theme. It will show given drawable instead of the white screen.

Related