繁体   English   中英

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

[英]Android, Splash screen shows next activity

我创建了SplashActivity.java > 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 ,而不是从启动MainActivity开始(由于内存)。

使用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));
}

}

手机可能存在内存不足的情况,因此当按下主屏幕按钮时,该应用会进入后台并且分配给它的内存可能会被释放(即,进程被终止),而在重新启动该应用时(长按主页按钮,或通过任何其他方法),从开始启动,并再次分配内存。

而且,如果您希望只在第一次在手机中启动应用程序时看到飞溅,请使用SharedPreferences

可能是您应该在应用程序中检查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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM