【问题标题】:Android, Splash screen shows next activityAndroid,启动画面显示下一个活动
【发布时间】:2026-01-17 09:25:01
【问题描述】:

我已经创建了 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,而不是从启动画面开始(因为内存)。

【问题讨论】:

  • 我认为你无法控制它。这一切都取决于Android。也许是因为内存不足,它正在杀死你的应用程序,当你启动时,它是从启动屏幕启动的。

标签: android android-activity splash-screen


【解决方案1】:

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

}

【讨论】:

  • 可能是您误解了这个问题。如果操作人员在开始安装时只希望启动一次,那么您的答案是有效的。但这里的问题是从后台启动屏幕重新启动应用程序后来了..
  • @kalyanpvs。当人注销时,可以将布尔值设置为false,这样每次用户注销成功后登录时,都可以看到闪屏。
  • 是的,通过在注销时将布尔值更改为 false,我们可以实现。但是这里没有注销功能 ..这里用户需要在 MainActivity(这是* Activity)onDestroy( ) 方法..
  • 有一个很大的问题,我想让我的splash作为第一个activity运行,因为当你运行MainActivity然后finish()它运行SplashActivity时,它会面临transactionAnimation 在许多设备中自动激活。我的意思是用户会理解这种变化。
【解决方案2】:

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

如果您希望只在第一次在手机中启动应用时看到启动画面,请使用SharedPreferences

【讨论】:

    【解决方案3】:

    也许你应该在你的应用程序中检查你的 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);
    }
    

    【讨论】:

    • 是的,它有效。但是这里如何处理一次只显示启动画面的要求呢?
    • 没有必要处理这个,因为在开始时这个活动会出现并在 1000 毫秒后结束。那么应该没有活动可以再次调用闪屏活动。如果有问题,这可能是内存问题,当单击主页按钮时,由于内存不足,操作系统会杀死您的应用程序,当您返回应用程序时,您的应用程序会从头开始。
    • 我的代码完全一样!但我的飞溅是透明的!!!主要活动显示在后面。我还检查了你的代码芽没有工作。
    • 我听不懂你在说什么?您的启动画面是透明的,但也显示了您背后的主要活动。这意味着当启动启动时,您的主要活动已经开始。也许您可以先尝试完成(),然后在启动画面中尝试 startActivity(intent)。
    最近更新 更多