I have splash screen in an application. When user is at home screen and presses the back button; an alert dialog with "Do you want to exit?" is appeared and if users slects OK then moveTaskToBack(true);
is called: and the app will exit. But on immediate launch of the application the splash screen is not shown. How to show splash screen on every launch of the application?

- 2,864
- 8
- 40
- 48
-
1onclick of ok just put `finish();` – Samir Mangroliya Sep 19 '12 at 14:33
-
SplashScreen! really...? not a good idea :) – waqaslam Sep 19 '12 at 14:34
-
2Generally a bad idea in Android. But you can define splash as the launch activity in your manifest. Of course, if a user pauses an activity, it will resume at that activity. You can check onResume, but be sure to remove that activity from your navigation stack before displaying splash. – Oh Danny Boy Sep 19 '12 at 14:41
-
Don't have splashscreen. you cannot even be really sure as of when you 'application' starts. – njzk2 Sep 19 '12 at 14:42
4 Answers
A few pointers on how to make a splash screen.
Say we have SplashActivity
and HomeActivity
.
SplashActivity
should be your launcher activity (in the manifest).
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
When you start HomeActivity
you should call finish()
on SplashActivity
so that you wouldn't return to it upon clicking the back button.
In HomeActivity
when you intercept the back button to show the dialog, don't call moveTaskToBack(true);
on OK because this only moves your application to the background and doesn't terminate it.
You should call finish()
instead. And since you already called finish on SplashActivity
, the only activity left is HomeActivity
, so your application will close.

- 25,285
- 12
- 54
- 61
-
the problem is that User can visit the Home screen many times in an application run so if the home screen is visited from some other activity and called finish() the application does not exit and shows the other last activity. – aman.nepid Sep 19 '12 at 14:50
-
You can solve this by [clearing the backstack](http://stackoverflow.com/a/5794572/1117415) when starting `HomeActivity` from other activities. – Benito Bertoli Sep 19 '12 at 14:58
-
My application is like Splash > Home > ... other activities. I called moveTaskToBack(true); from Home. When I relaunch the application from application launcher in device the Splash is not shown it directly goes to Home. So how to show Splash in this scenario? – aman.nepid Sep 19 '12 at 16:35
Ideally You should not do that .You should respect user expectations for behavior of apps.When he clicks on your app it should start from where he left . If the app is killed due to memory constraints then it will start a fresh .
Still if you want to do it : Source : Close application and launch home screen on Android
Intent intent = new Intent(this, FinActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
Inside FinActivity : call finish() in onCreate.

- 1
- 1

- 2,806
- 4
- 26
- 38
I think you SplashScreen is only shown when the App is starting.
So if the Acitivites aren't finished but just paused the SplashScreen will not be shown because the App wasn't really closed.
You can either finish the application when back button is pressed (Code: this.finish()
) or check on every Activity's onResume()
-Method if the App was in Background and show the SplashScreen if true.

- 5,070
- 2
- 28
- 51
I feel the only reason splash screens are ever necessary are for the launch of an application and if you want to load some information through a thread while doing so, but that is just my opinion.
Anyways, you should be launching your splash screen as the first activity in your manifest, then after you would launch an intent that will start your next activity?(The actual main). There is a great tutorial I used a couple of months ago when I was first dealing with these, It goes through it with you STEP by STEP. I hope this helps :)

- 1,564
- 3
- 29
- 46