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:
- on Android back button press, the
SplashScreenwont be displayed. So no problems here! - on up navigation on
ActionBaris pressed, theSplashScreenis displayed again. This is the problem, I don't want theSplashScreendisplayed 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!
I ended up using
SharedPreferncesto store a key forSplashNotDisplayedand reading it everytimeMainActivityis loaded.I set the Key to
Trueinmyapplication.javaas this is only loaded once for every app instance and once the splash is displayed I set the key toFalseand the next time ifMainActivityis loaded, SplashNotDisplayed key will be false and it wont be displayed again.in
MyApplication.javain
MainActivity.javaI changed the implementation from thisto this: