Kill Splash Screen When Click “BACK” Button

2019-06-10 06:03发布

问题:

I have created a splash screen for my android app. My question is simple. After 5 seconds splash screen disappear and main activity works. Then if i click "Back" button it returns splash screen again. But i don't want this.

If user touch "Back" button on mainactivity, app must go android menu without splash screen. How can i fix it?

package com.example.androidfirst;

import android.app.Activity;

public class SplashActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.splash_screen);

    Thread timer = new Thread() {   //new Thread
        @Override
        public void run() {

            try {
                sleep(5000);
            }
            catch (InterruptedException e) {

                e.printStackTrace();
            }
            finally {
                Intent MainAct = new Intent("com.example.androidfirst.MAINACTIVITY");
                startActivity(MainAct);

                try {
                    this.finalize();
                }
                catch (Throwable e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }

    };

    timer.start();

}

}

回答1:

If user touch "Back" button on mainactivity, app must go android menu without splash screen. How can i fix it?

You might find using a Handler and Runnable an easier way to create a delay, but simply call finish() after startActivity().



回答2:

You can implement in your AndroidManifest.xml with android:noHistory="true" in <activity> tag for Splash Screen.



回答3:

First of all, don't use splash screens.

Nevertheless, you have to call finish() and or start the new activity with Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK.



回答4:

You'll want to add onStop() to specify what to be done when the splash screen is no longer visible.

Call finish() from there.



回答5:

I would strongly advise you against using a splash screen just for the sake of having a splash screen.

If you've got some long running task that you need to do before you are ready to show the UI to the user (i.e. loading data from some source), then showing a splash while you are loading that data is appropriate. Otherwise you are simply wasting your users time.

5 seconds doesn't seem like a long time, but while your users are sitting there staring at a useless splash screen for that time it will seem like an eternity to them.

If you feel like you must use a splash screen please read and absorb the knowledge on this page: SplashScreens Done Right

Once you've read that follow the examples that he uses so as to at least annoy your users as little as possible.