Sobes.tech
Junior — Middle

What ways are there to finish an Activity in an Android app?

sobes.tech AI

Answer from AI

In Android, to finish an Activity, several methods can be used:

  1. Call finish() method — the standard way to programmatically close an Activity. After calling finish(), the Activity is destroyed and removed from the stack.
// Finish current Activity
finish();
  1. Handle the system "Back" button — when the "Back" button is pressed, onBackPressed() is called, which by default calls finish(). You can override this method for custom behavior.

  2. Finish Activity from another component — you can send an Intent with flags or use methods to close the Activity externally.

  3. System termination — if the system is low on resources, it may destroy the Activity without calling finish(), but this is not controlled directly.

  4. Finish with FLAG_ACTIVITY_CLEAR_TOP — when starting an Activity with this flag, all Activities above it in the stack are closed.

Example of calling finish():

public void onClickClose(View view) {
    finish();
}
What ways are there to finish an Activity in an… - sobes.tech