从设置活动返回时不显示启动屏幕

时间:2018-07-14 15:28:54

标签: android-actionbar settings splash-screen back-button main-activity

我有一个应用程序将SplashScreen加载为全屏Dialog,并在加载MainActivity时关闭。但是,这里的问题是,每当我打开SettingsActivity并通过以下方式回到MainActivity时:

  1. 在按Android后退按钮时,不会显示SplashScreen。所以这里没问题!
  2. ActionBar上的向上导航中,按下SplashScreen会再次显示。这是问题,我不希望再次显示SplashScreen

这是我的MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setPreferencesFromSettings();
    if (splashScreenDisplayed) {showSplash(); splashScreenDisplayed = false; }
    //...DO SOMETHING ELSE...//
}

public void showSplash() {
    final Dialog dialog = new Dialog(MainActivity.this, android.R.style.Theme_Light_NoTitleBar);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.activity_splash_screen);
    dialog.setCancelable(true);
    dialog.show();

    final Handler handler  = new Handler();
    final Runnable runnable = new Runnable() {
        @Override
        public void run() {
                dialog.dismiss();
        }
    }; handler.postDelayed(runnable, 3500);
}

@Override
public void onRestart() {
    super.onRestart();
    splashScreenDisplayed = false;
    setPreferencesFromSettings();
}

还有AndroidManifest.xml

<application
    android:name=".AnalyticsApplication"
    android:fullBackupContent="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity
        android:name=".SettingsActivity"
        android:label="Settings"
        android:parentActivityName=".MainActivity"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>
</application>

我尝试了其他一些帖子,但是它们对我没有用。我知道我缺少一行代码或某处东西,我花了数小时反复查看代码,但找不到任何东西。

在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我最终使用SharedPrefernces存储SplashNotDisplayed的密钥,并在每次加载MainActivity时读取它。

我在True中将密钥设置为myapplication.java,因为对于每个应用程序实例仅加载一次,并且在显示启动画面后,我将密钥设置为False,下次将密钥设置为MainActivity MyApplication.java已加载,SplashNotDisplayed键将为false,并且不会再次显示。

SharedPreferences prefs = getSharedPreferences("prefs", MODE_PRIVATE); SharedPreferences.Editor shPrefEditor = prefs.edit(); shPrefEditor.putBoolean("splashNotDisplayed", true); shPrefEditor.apply();

MainActivity.java

if (splashScreenDisplayed) {showSplash(); splashScreenDisplayed = false; } 中,我从此更改了实现

boolean splashNotDisplayed = prefs.getBoolean("splashNotDisplayed", true);
if (splashNotDisplayed) {
    showSplash();
    shPrefEditor.putBoolean("splashNotDisplayed", false);
    shPrefEditor.apply();
}

对此:

installer status |grep Version| cut -c12-13