Android 如何在应用程序上显示一次性启动屏幕?

Android 如何在应用程序上显示一次性启动屏幕?,android,android-layout,splash-screen,Android,Android Layout,Splash Screen,如何在用户安装我的应用程序时显示启动屏幕。我不想每次用户打开它时都显示闪屏,但仅当用户在手机上安装并首次打开它时才显示闪屏。如何做到这一点 每次打开应用程序时,您都需要检查这是应用程序的首次启动吗?如果是,则显示一次性启动屏幕,否则显示主要活动 您可以使用共享首选项来存储有关首次启动的数据。在每次启动应用程序时显示的主要活动中,请尝试以下逻辑 SharedPreferences mPrefs; final String splashScreenPref= "SplashScreenShown

如何在用户安装我的应用程序时显示启动屏幕。我不想每次用户打开它时都显示闪屏,但仅当用户在手机上安装并首次打开它时才显示闪屏。如何做到这一点

每次打开应用程序时,您都需要检查这是应用程序的首次启动吗?如果是,则显示一次性启动屏幕,否则显示主要活动


您可以使用共享首选项来存储有关首次启动的数据。

在每次启动应用程序时显示的主要活动中,请尝试以下逻辑

 SharedPreferences mPrefs;
 final String splashScreenPref= "SplashScreenShown";


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mPrefs = PreferenceManager.getDefaultSharedPreferences(this);


    Boolean splashScreenShown= mPrefs.getBoolean(splashScreenPref, false);

    if (!splashScreenShown) {            
        Intent intent=new Intent(MainActivity.this,SplashScreenActivity.class);
        startActivity(intent);

        SharedPreferences.Editor editor = mPrefs.edit();
        editor.putBoolean(splashScreenShown, true);
        editor.commit(); 
        finish();
    }

}
请参阅以下链接,以更好地了解如何使用SharedReferences:, 这里

您应该创建应用程序类,并且需要从应用程序类onCreate方法调用require活动

    public class Appli extends android.app.Application {
        @Override
        public void onCreate() {
            super.onCreate();
//manage base on your requirement,you can use share preference for splash screen track
            Intent intent = new Intent(this,Main2Activity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            startActivity(intent);

        }
舱单:

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:name=".Appli"
        android:theme="@style/AppTheme">
</application>


以上代码确实有效,我已经测试过。

可能重复,或者您可以使用共享首选项。