Question

In my application I have a SplashScreen which is opened when the app is launched. And after 1 second the Login activity is opened. But I have an issue when the HOME button is pressed right after the app is launched. If I tap on the HOME button, only when the splash screen is visible, the app closes but after a few seconds (aprox. 2 sec) the Login activity opens even if the app is not visible anymore (it is alive only in the back stack).

Here is how I start the Login activity:

Handler handler = new Handler();
        handler.postDelayed(new Runnable() {

            public void run() {
                // Show login screen
                Utility.startLoginActivity(SplashScreenActivity.this);

            }
        }, 1000);

And this is the startLoginActivity() method:

 Intent intent = new Intent(activity, LoginActivity.class);
 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 activity.startActivity(intent);
 // Finish the calling activity
 activity.finish();

How could I fix this?

Was it helpful?

Solution

You should add check if splash activity is visible:

Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        public void run() {
            if (SplashActivity.isVisible()) {
                // Show login screen
                Utility.startLoginActivity(SplashScreenActivity.this);
            }
        }
    }, 1000);

SplashActivity.isVisible() method can be implemented as described here: How to check if activity is in foreground or in visible background?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top