Android,Splash屏幕显示下一个活动

时间:2014-06-10 09:42:02

标签: android android-activity splash-screen splash

我创建了Splash - > SplashActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_splash);
    pref = new Prefs(getApplicationContext());
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                startActivity(new Intent(SplashActivity.this,
                        MainActivity.class));
                SplashActivity.this.finish();
            }
        }, 1000);
}

这是一个问题,当我按Home_Button然后再次运行应用程序退出应用程序时,Splash再次出现(我不想再看到它)。

即使我检查了onPauseonStop事件,但它也无法正常工作。

修改

我的启动是透明的,问题是在后面显示MainActivity,而不是从启动(因为内存)开始。

3 个答案:

答案 0 :(得分:0)

使用SharedPreferences存储一个标志,表示您的启动已经显示。在启动画面的onCreate()方法中查看它是否存在,启动下一个活动。

一些参考代码:

SharedPreferences mPrefs;
final String splashDisplayedSharePref = "splashScreenShown";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

// second argument is the default to use if the preference can't be found
Boolean splashDisplayed = mPrefs.getBoolean(splashDisplayedSharePref, false);

if (!splashDisplayed) {
    // here you can launch another activity if you like
    // the code below will display a popup

    SharedPreferences.Editor editor = mPrefs.edit();
    editor.putBoolean(splashDisplayedSharePref, true);
    editor.commit(); // Very important to save the preference
new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            startActivity(new Intent(SplashActivity.this,
                    MainActivity.class));
            SplashActivity.this.finish();
        }
    }, 1000);

}
else
{
// Splash displayed. Directly start the next activity.
startActivity(new Intent(SplashActivity.this,
                    MainActivity.class));
}

}

答案 1 :(得分:0)

您的手机可能存在内存不足的情况,因此当按下主页按钮时,该应用会进入后台和分配给它的内存可以被解除分配(即进程被终止),当你重新启动应用程序时(从长按主页按钮,或通过任何其他方法),它从一开始就被启动&再次分配内存。

如果您需要,只有在首次使用手机启动应用时才能看到启动,请使用SharedPreferences

答案 2 :(得分:0)

您可能应该检查应用中的onResume()方法。  本规范对我来说非常有效,没有任何其他代码:

// Splash screen timer (3 seconds)
private static int SPLASH_TIME_OUT = 1000;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.splash_screen);

    new Handler().postDelayed(new Runnable() {

        /*
         * Showing splash screen with a timer. This will be useful when you
         * want to show your application logo or company logo
         */

        @Override
        public void run() {
            // This method will be executed once the timer is over

            Intent intent = new Intent(SplashScreen.this, MainActivity.class);

            // Close this activity
            finish();

            startActivity(intent);              
        }
    }, SPLASH_TIME_OUT);
}