Francesco Francesco - 23 days ago 8
Android Question

Android activity closes two times

i have an android app and it works pretty well but just recently it started to show a problem.
The problem is that it starts normally but when i click back it creates a new main activity and when i click back another time the application closes correctly .

Here there's a video of that: https://streamable.com/s/s7m8c/snygay?muted=1

I can't get ahead of this problem by myself so does anyone knows why it happens?

Edit:
The activity is launched with this code.

mHandler = new Handler();
mActivityLaunchRunnable = new Runnable() {
@Override
public void run() {
// This method will be executed once the timer is over
// Start app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
};
mHandler.postDelayed(mActivityLaunchRunnable, SPLASH_TIME_OUT);
initialTime = System.currentTimeMillis();


And the onBackPressed is not overridden

Answer Source

you need to stop adding the splash screen activity to the stack. you can do this this like below in your manifest:

 <activity
android:name=".SplashScreen"
android:noHistory="true" />

then you can remove finish() in your code snippet.

note: another way to do this is to use FLAG_ACTIVITY_NO_HISTORY