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:
- Call
finish()method — the standard way to programmatically close an Activity. After callingfinish(), the Activity is destroyed and removed from the stack.
// Finish current Activity
finish();
-
Handle the system "Back" button — when the "Back" button is pressed,
onBackPressed()is called, which by default callsfinish(). You can override this method for custom behavior. -
Finish Activity from another component — you can send an Intent with flags or use methods to close the Activity externally.
-
System termination — if the system is low on resources, it may destroy the Activity without calling
finish(), but this is not controlled directly. -
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();
}