I totally agree with you that finish()
is somehow not practical but anyways you can use it simply and all will be "kosher" with best practices I suppose.
Here is a short tutorial for you to handle "Exit" from any other activity in your app:
I assume that splash screen is the first activity in your app.
- If you also have a main/home screen in your app then finish the splash activity right after the main/home has been called with Intent from your Splash activity with e.g.
startActivity(new Intent(getApplication(), Home.class));
Splash.this.finish();}
After doing so, follow the step 2. but instead of overriding the onResume in splash screen override it in the Home/Main screen.
2. If your user needs access to the splash screen later, do not finish()
your splash screen but override its onResume method as follows:
@Override
protected void onResume() {
super.onResume();
try {
Intent i = getIntent();
Boolean isExit = i.getBooleanExtra("isExitAction",false);
if(isExit){
this.finish();
}
}
catch (Exception e){}
}
3. In the activity from which you want to close the application, use the following code to exit where it suits you the most. Do not forget to substitute for Home.class
depending which is your first unfinished activity.
Intent i = new Intent(this, Home.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("isExitAction", true);
startActivity(i);
WHAT IT DOES: If you press a button from your 5th activity which fires the code in the 3rd step it finishes all previous activities 4,3,2 and "redirects" you to activity 1, which is your Home (or Splash).
After the redirection, the onResume()
is called, and it finishes the last remaining activity. The application reaches this.finish()
only if isExitAction
is true