Pergunta de entrevista da empresa Trantor

What is the Launch mode in activity?

Resposta da entrevista

Sigiloso

14 de dez. de 2024

The launch mode in an Android activity defines how the activity is instantiated and brought to the front when the user navigates to it. It determines whether a new instance of the activity is created or if the current instance should be reused. The launch mode is set in the activity's entry and has several modes: Launch Modes standard (default) Behavior: A new instance of the activity is created each time it is started, regardless of whether it is already in the activity stack. Use Case: Activities where each invocation should be treated as a completely new session (e.g., home screen). singleTop Behavior: If the activity is already at the top of the task stack, it is reused; otherwise, a new instance is created. The onNewIntent() method is called when the activity is reused. Use Case: Activities that only need to be brought to the foreground and updated with new data without creating a new instance (e.g., chat message updates). singleTask Behavior: Only one instance of the activity is allowed in the task stack. If an instance of the activity exists in the stack, it is reused. Otherwise, a new instance is created. Use Case: Activities that should act as a singleton within a task, managing state and data across multiple activations (e.g., shopping cart). singleInstance Behavior: The activity is launched in its own task, independent of any other activities. No other activity can be in the same task as a singleInstance activity. Use Case: Activities that should be completely independent of other activities (e.g., settings screen).