android-actionbarsettingssplash-screenback-buttonmain-activity

Do not show splash screen on return from settings activity


I have an app that loads a SplashScreen as a full screen Dialog that closes on loading the MainActivity. However, the problem here is that whenever I open my SettingsActivity and come back to the MainActivity in the following ways:

  1. on Android back button press, the SplashScreen wont be displayed. So no problems here!
  2. on up navigation on ActionBar is pressed, the SplashScreen is displayed again. This is the problem, I don't want the SplashScreen displayed again.

Here's my 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();
}

And the 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>

I tried a few other posts however they didn't work for me. I know that I am missing a line of code or something somewhere, I spent hours looking at the code again and again and I couldn't find anything.

Thanks in advance for your help!


Solution

  • I ended up using SharedPrefernces to store a key for SplashNotDisplayed and reading it everytime MainActivity is loaded.

    I set the Key to True in myapplication.java as this is only loaded once for every app instance and once the splash is displayed I set the key to False and the next time if MainActivity is loaded, SplashNotDisplayed key will be false and it wont be displayed again.

    in MyApplication.java

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

    in MainActivity.java I changed the implementation from this

    if (splashScreenDisplayed) {showSplash(); splashScreenDisplayed = false; }
    

    to this:

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