newbee
newbee

Reputation: 91

Android back stack and Memory reclaim

I am learning android app development. I am reading about how activities are managed by the system.

By reading this documentation. Here is the confusion.

  1. I create app A (from home screen) with 3 activities A1, A2, A3 [this is Task A]
  2. I create app B (from home screen) with 3 activities B1, B2, B3 [this is Task B] where A1 and B1 are the top of the stack in app A and B respectively.

As per the documentation system only pushes and pops the activity (No reordering, which makes sense for a stack) But, it states system can reclaim memory in case not enough memory is available. And to pick which activity to remove system always picks the one which is in background. So say its removing activity A3. But A3 is at bottom of the stack. How can it remove A3 without reordering the stack for app A?

I hope I am clear with my question

Thank You,

Upvotes: 3

Views: 747

Answers (2)

sultan.of.swing
sultan.of.swing

Reputation: 1116

Android system doesn't reorder the stack as you correctly pointed out. It follows the Process Lifecycle where the process which contains the activity is terminated and not individual activities on the stack. And remember that by default, every application is a separate process in which the activity stack is maintained. So the activity stack of app A and app B will be separate and independent.

For example Task A is in the background with A1, A2 and A3 activities. Task A is a process. When android needs to reclaim memory, it will terminate the process Task A which contains all the activities A1, A2, A3. So it does not matter which activity is at the top of the stack in Task A at the moment.

Upvotes: 0

ianhanniballake
ianhanniballake

Reputation: 199805

The back stack is not changed when an activity is removed from memory.

However, when the user hits the back button and goes back to an activity that has been reclaimed, then any state you do not specifically save in your onSaveInstanceState() (and reload from the savedInstanceBundle argument in onCreate()) will be lost.

Upvotes: 1

Related Questions