I faced a similar situation. As I understand it, even if I call the finish() function at the beginning, the entire onCreate() function is executed. Then he will finish. In my script, in the onCreate() function, I call another action. That way, even if the main action completes after onCreate() , the second action still exists. So my decision was,
private boolean finish = false;
in onCreate () of the main function,
if(intent.getBooleanExtra("EXIT", false)) { finish(); finish = true; }
and I checked all the navigation files
if(!finish) { Intent intent = new Intent(MainActivity.this, LoginActivity.class); startActivity(intent); finish(); }
My exit function was
Intent intent = new Intent(getApplicationContext(), MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra("EXIT", true); startActivity(intent);
source share