I have designed an app with a splash screen that sleep() s for 3 seconds and displays the Home screen of my app. I can navigate into my app seamlessly and after i come back to Home screen, when Back button is pressed the control goes back to Splash screen again instead of terminating the app. Please give me a solution. :)
-
Please show the code where you are starting the `Home` activity from Spalsh activity. – Shobhit Puri Jul 31 '13 at 04:52
6 Answers
Since you've not posted the code, I am guessing the you did not call finish();
. You may call it inside your onPause()
or before calling the new Intent as other's have suggested.
Update
If you are just loading a splash screen, you might just set the parameter to not keep it in activity stack. In your manifest.xml, where you define your activity do:
<activity android:name=".SplashScreen" android:noHistory="true" ... />
You won't require to class finish()
. Just normally call startActivity()
.
See: How to finish current activity in Android
Calling finish() After Starting a New Activity
Start new Activity and finish current one in Android?
Hope this helps.

- 1
- 1

- 25,769
- 11
- 95
- 124
Finish your splash activity before starting the new one. The onResume
method of your splash activity could be something like this:
@Override
protected void onResume() {
super.onResume();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// finish the splash activity so it can't be returned to
SplashActivity.this.finish();
// create an Intent that will start the second activity
Intent mainIntent = new Intent(SplashActivity.this, SecondActivity.class);
SplashActivity.this.startActivity(mainIntent);
}
}, 3000); // 3000 milliseconds
}

- 16,921
- 10
- 60
- 69
Change your splash activity xml as below by adding android:noHistory="true" and you are done.
<activity
android:name="com.naveen.example.SplashActivity"
android:label="@string/app_name"
android:noHistory="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

- 830
- 9
- 19
Before you start or let's say go to new Activity, finish old activity, like:
YourSplashActivity.this.finish();
Intent intent = new Intent(SplashActivity.this, SecondActivity.class);
YourSplashActivity.this.startActivity(intent);
In addition you can do followed logic:
// drop application to home activity (to prevent to show Splash)
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

- 77,483
- 27
- 203
- 225
Assuming that your splash screen will only appear once when your application starts and will not show again until you have restarted your app after the app has finished running(meaning exiting the app by pressing the home button does not count), why not call finish() on overridden onPause() of your splash screen activity like the following.
@Override
public void onPause() {
super.onPause();
finish();
}
It's much simpler than running a thread or posting a Runnable object for the handler to handle. I have tested it and it seems to work. I don't know if there is any drawback to this solution but if there is any issue I overlooked I wish somebody would point it out for me.

- 167
- 1
- 2
- 6
Just for the record: With the solutions described here you're wasting time because they pause the initialization for 2-3seconds before they continue.
I prefer adding a Splash Screen Layout
on top of my main_activity.xml
. I detect the first start of the application by extending Application. If it´s the first start, I show my Splash-Screen while the UI is build in the background... (Use background threads if the ProgressBar lags!)
//Extend Application to save the value. You could also use getter/setter for this instead of Shared Preferences...
public class YourApplication extends Application {
public static final String YOUR_APP_STARTUP = "APP_FIRST_START";
@Override
public void onCreate() {
super.onCreate();
//set SharedPreference value to true
SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = mPreferences.edit();
editor.putBoolean(YOUR_APP_STARTUP, true);
editor.apply();
...
}
Check for your first start in your MainActivity
public class YourMainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//hide actionbar and other menu which could overlay the splash screen
getActionBar().hide();
setContentView(R.layout.activity_main);
Boolean firstStart = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getBoolean(TVApplication.YOUR_APP_STARTUP, true);
if (firstStart) {
//First app start, show splash screen an hide it after 5000ms
final RelativeLayout mSplashScreen = (RelativeLayout) findViewById(R.id.splash_screen);
mSplashScreen.setVisibility(View.VISIBLE);
mSplashScreen.setAlpha(1.0f);
final FrameLayout mFrame = (FrameLayout) findViewById(R.id.frame_container);
mFrame.setAlpha(0.0f);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Animation fadeOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.fade_out_animation);
fadeOutAnimation.setDuration(500);
fadeOutAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
mFrame.setAlpha(1.0f);
getActionBar().show();
}
@Override
public void onAnimationEnd(Animation animation) {
mSplashScreen.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mSplashScreen.startAnimation(fadeOutAnimation);
}
}, 5000); //<-- time of Splash Screen shown
} else {
((RelativeLayout) findViewById(R.id.splash_screen)).setVisibility(View.GONE);
getActionBar().show();
}
Insert the SplashScreen at top in your main.xml. I prefer RelativeLayout
for that. In the example, SplashScreen is placed on to of a layout with the Navitgation Drawer
, which we really love, don`t we?
//main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content view -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer list -->
<ListView
android:id="@+id/slider_list"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_gravity="start"
android:background="@color/tvtv_background"
android:choiceMode="singleChoice"
android:divider="@drawable/nav_bar_divider"
android:dividerHeight="1dp"
android:listSelector="@android:color/transparent" />
</android.support.v4.widget.DrawerLayout>
<RelativeLayout
android:id="@+id/splash_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:background="@color/tvtv_white"
android:visibility="visible" >
<ImageView
android:id="@+id/splash_screen_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/splash_screen_text"
style="@style/TVTextBlueContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/splash_screen_logo"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:text="Awesome splash shiat" />
<ProgressBar
android:id="@+id/splash_screen_loader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/splash_screen_text"
android:layout_centerHorizontal="true"
android:clickable="false"
android:indeterminate="true" />
</RelativeLayout>
</RelativeLayout>

- 11,104
- 10
- 55
- 89