Junior — Middle
Can you list different launch modes of activities or fragments in Android and explain their features?
sobes.tech AI
Answer from AI
In Android, there are different launch modes for activities that determine how the system manages Activity instances in the task stack. The main launch modes are:
-
standard (default)
- A new instance is created each time the activity is launched, even if one already exists in the stack.
-
singleTop
- If the top element of the stack is already an instance of this activity class, a new instance is not created, and the
onNewIntent()method is called. - If the activity is not at the top of the stack, a new instance is created.
- If the top element of the stack is already an instance of this activity class, a new instance is not created, and the
-
singleTask
- Only one instance of this activity class can exist in the task stack.
- If the activity already exists in the stack, the system brings it to the top and calls
onNewIntent(), removing all activities above it.
-
singleInstance
- Similar to singleTask, but the activity is launched in a separate task, and only this activity can exist in that task.
There are no explicit launch modes for fragments, but managing their lifecycle and transaction stack allows control over navigation behavior.
Example in AndroidManifest.xml for an activity with the singleTop mode:
<activity android:name=".MyActivity"
android:launchMode="singleTop" />
These modes help optimize app behavior and manage navigation between screens.