Activity stack

A task is a collection of activities that users interact with when performing a certain job.

The activities are arranged in a stack. There can be one or multiple activity stacks visible on screen.

When a new activity is started, it is usually placed on the top of the current stack and becomes the running activity. The previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

If the user presses the Back button, this new action is completed and popped from the stack. You can override the default behavior of the Back button. For example, show a dialog that asks the user if he is sure he want to close the window.

If the user leaves a task for a long time, the system clears the task of all activities except the root activity. When the user returns to the task again, only the root activity is restored. The system behaves this way, because, after an extended amount of time, users likely have abandoned what they were doing before and are returning to the task to begin something new.

launch activity

An Intent is an abstract description of an operation to be performed. It can be used with startActivity() method of context to launch an Activity.

class MainActivity : AppCompatActivity() {
   // ...
   fun onNewMyActivity(v: View) {
       startActivity(Intent(this, OtherActivity::class.java))
   }
}

launch modes

There are modes how to launch activities. You can specify mode in AndroidManifest.xml file as property of activity.

<activity android:name=".MyActivity"
        android:launchMode="singleInstance">
</activity>
modes description
"standard"

Default mode. The system creates a new instance of the activity in the task from which it was started.

The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances.

"singleTop"

If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity.

The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the activity at the top of the back stack is not an existing instance of the activity).

"singleTask"

The system creates a new task and instantiates the activity at the root of the new task. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance. Only one instance of the activity can exist at a time.

"singleInstance" Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task; any activities started by this one open in a separate task.

Or you can specify mode as intent flag in code when you start activity.

class MainActivity : AppCompatActivity() {
// ...
fun onNewMyActivity(v: View) {
    val intent = Intent(this, OtherActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
                .or(Intent.FLAG_ACTIVITY_SINGLE_TOP)
        }
    startActivity(intent)
}}
flag description
FLAG_ACTIVITY_NEW_TASK Same as "singleTask" launchMode.
FLAG_ACTIVITY_SINGLE_TOP Same as "singleTop" launchMode.
FLAG_ACTIVITY_CLEAR_TOP

If the activity being started is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it are destroyed and this intent is delivered to the resumed instance of the activity (now on top), through onNewIntent()

It is most often used in conjunction with FLAG_ACTIVITY_NEW_TASK.

Regardless of whether an activity starts in a new task or in the same task as the activity that started it, the Back button always takes the user to the previous activity.