Android: Show AllertDialog before the application starts

Victor Apoyan
3 min readAug 10, 2020

The Problem

Show an AlertDialog while the application is starting, or after the application is finished. The key here is that AlertDialog should look like the System Dialog, means there should be nothing behind it.

it seems the solution should be an easy one … right? 🤔

The Attempts to Solve

My first thought was to show an AlertDialog right from the application onCreate() method, something like this

Easy, right? 🤯 Unfortunately, this doesn’t work at all, throwing an exception

You need to use a Theme.AppCompat theme (or descendant) with this activity.

Digging deeper into the Alert Dialog documentation 🧐, I found out that I can set the theme in the constructor, so I did.

Yuhoooo 🥳, I thought now everything will work as I expect, but no, again, exception

Unable to add window — token null is not valid; is your activity running?

Of course, my activity is not running, you idiot 😬

Digging deeper into the google search results, I saw some recommendations to set dialog type to SYETM_ALERT and also add some user permission, so I did it

🛑 Don’t hurry to copy-paste this code, it also doesn't work. I got another exception

permission denied for window type 2003

Come on, unbelievable, it should work 😟 … I want to do a simple thing … but no way, next hour was killed by searching and trying out different stuff, like usingWindowManager.LayoutParams.TYPE_TOAST, TYPE_APPLICATION_OVERLAY, TYPE_PHONE instead of TYPE_SYSTEM_ALERTalso does not fix the situation.

The problem is that AlertDialog is not designed to work out of activity context, you need to feed him activity context, so that it can be shown.

So if AlertDialog needs the activity, then it will get an activity…

The Solution 😇

Okay, to show AlertDIalog, we need activity, but my talk was to show the ONLY alert dialog and create an impression for the user that application is even not yet started. So how to solve this…

What about a transparent activity which will show the dialog … hmm, should work.

Let’s create a style for transparent activity

so we need activity which will have a transparent background, no animation will be performed when starting and ending, no title and basically nothing, it should be as much invisible as possible.

Let’s apply the theme to the activity

And here is the activity itself

Right in the onCreate() method, we show the AlertDialog.

And finally, we need to start our activity from the Application onCreate() method

Please note that you need to add a flag FLAG_ACTIVITY_NEW_TASK, because otherwise, you will get an exception

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

What is missing now is our AlerDialog is still shown with animation, yes we specified in the style that we don’t want animation by setting windowAnimationStyle to @null but this doesn’t work.

To make our implementation complete we can add overridePendingTransition(0, 0) to the TranslusentActivity onStart() and onPause() methods.

After that, we will have something like this

I hope I was able to help someone who tries to solve a similar problem, if you have a better solution for this, post in the comments or just email me, always happy to discuss and improve :)

--

--