Needs clarification for closing gracefully the Android application

44 views Asked by At

I want to close the application gracefully. I found two methods.

1. Using Intents:

Intent intent = new Intent(Intent.ACTION_MAIN);
 intent.addCategory(Intent.CATEGORY_HOME);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent);

Documentation says:

Activity Action: Start as a main entry point, does not expect to receive data.

Category Home: This is the home activity, that is the first activity that is displayed when the device boots.

2. Using Finish:

finish()

Documentation says:

Call this when your activity is done and should be closed.

What is the best method or professional method for closing the Android application? Both close the application but finish() removes the app from cache (App not in recent activities) while using intent, cache does not deleted. Should cache remove from the cell on finishing the activity?

1

There are 1 answers

0
S.Thiongane On

The best way is the proper one, as it's said in the official doc :

Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().

The App is a succession of activities. Closing the app means closing the first activity of the app.

The first method you described in your Post (1. Using Intents) seems to be a workaround; it's similar to when the physical home button of your device is pressed. It lets the activity to the back stack and launch the Home (First) activity of the device.