What is an Activity Stack? Explain it with the help of a diagram.

An Activity Stack is a key concept in Android development that represents the way activities are organized and managed within an application. It refers to the collection of activities in a task, where the last added activity is on the top of the stack.

Imagine an Android application with multiple activities, A, B, and C, where A is the initial/main activity. When the application starts, Activity A is placed at the bottom of the stack. Then, let's say the user navigates to Activity B from A, resulting in Activity A moving down the stack and Activity B becoming the topmost activity.

To provide a visual representation, consider the following diagram:

| Activity C |
| Activity B |
| Activity A |

In this example, Activity C is the topmost activity on the stack, Activity B is in the middle, and Activity A is at the bottom.

Now, let's say the user navigates from Activity B to Activity A. This transition will cause B to be popped from the stack, as the back button is pressed. The stack will then look like this:

| Activity C |
| Activity A |

Now, if the user opens a new activity, Activity D, from Activity A, the stack will be updated as follows:

| Activity C |
| Activity A |
| Activity D |

Activity D becomes the topmost activity, while Activity A is pushed down but remains in the stack.

The Activity Stack allows Android to manage the order of activities, handle the back button behavior, and maintain the state of each activity within a task. It helps in maintaining a logical flow of screens and simplifies navigation within an application.