本文介绍了如何仅在应用程序启动“新鲜"时显示启动画面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在应用程序生命周期中只显示一次启动画面.这是我的代码:

I want to show the splash screen only once during the Application life cycle. Here is my code:

SplashScreenActivity.java:

SplashScreenActivity.java:

final int welcomeScreenDisplay = 3000;

Thread welcomeThread = new Thread() {

    int wait = 0;

    @Override
    public void run() {
        try {
            super.run();

            while (wait < welcomeScreenDisplay) {
                sleep(1000);
                wait += 1000;
            }
        } catch (Exception e) {
            System.out.println("EXc=" + e);
        } finally {

            // Start other Activity
            startActivity(new Intent(SplashScreenActivity.this,
                    MainActiviey.class));
            finish();
        }
    }
};
welcomeThread.start();

清单:

    <activity android:name=".SplashScreenActivity" android:label="test"
    android:noHistory="true"
        android:screenOrientation="portrait" android:configChanges="orientation|keyboardHidden|keyboard">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".MainActiviey" android:label="test"
         android:screenOrientation="portrait"
        android:configChanges="orientation|keyboardHidden|keyboard">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

问题是,如果我按硬件 HOME 按钮隐藏应用程序并在应用程序列表中再次打开该应用程序.它将再次显示初始屏幕(而不是显示 MainActivity).是否可以仅在应用程序新鲜"启动时才显示启动画面(在 onresume() 时不显示)?谢谢!

The problem is if I press the hardware HOME botton to hide the app and open the App again at the application list. It will show the splash screen again (instead of showing the MainActivity).Is it possible to show splash screen only when the app starts "fresh" (not show at onresume() )? Thanks!

推荐答案

是的,这是可能的.使用 SharedPreferences 来存储一个标志,这将表明您的飞溅已经被显示.在启动画面的 onCreate() 方法中检查它,如果它存在,则启动下一个活动.

Yes, it's possible. Use SharedPreferences to store a flag, that would indicate that your splash has already been shown. Check it in onCreate() method of your splash screen and if it is present, launch the next activity.

这篇关于如何仅在应用程序启动“新鲜"时显示启动画面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 12:00
查看更多