Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Misbehaviour of Backstack of activity when activity destroyed

Tags:

I have two activities; let's say A and B. In activity A there is a broadcast receiver registered that listens for a particular event which will finish activity A. I am registering the broadcast receiver in onCreate(), and destroying it in onDestroy() of activity A.

For simplicity, there is one button in activity B named "Destroy Activity A". When a user clicks on button, activity A should be destroyed.

Normally all of this is running smoothly without any issues,but the problem occurs in following scenarios:

1) Suppose I am in activity B and i press the Home key to move the application to the background then if i use other resource-heavy applications, Android system will kill my application to free memory. Then If I open my application from recent tasks, activity B will be resumed, and it's onCreate(), onResume() etc method will be called. Now I press button to destroy activity A, but activity A has already been destroyed, so activity A's onCreate(), onResume() etc methods will not be called until and unless i go to activity A by pressing the back button. Thus broadcast receiver is not registered to listen for the event.

2) The same problem will arise when user has selected "Don't keep activities" from Developer options in the device's settings.

I have been looking to solve this issue for a long time, but i am unable to find a proper answer. What is the best way to handle this scenario? Is this an Android bug? There should be some solution for this issue.

Please help me.

like image 540
Smeet Avatar asked Apr 10 '15 12:04

Smeet


People also ask

What happens to the back stack when you switch between activities?

If the user presses or gestures Back, the current activity is popped from the stack and destroyed. The previous activity in the stack is resumed. When an activity is destroyed, the system does not retain the activity's state.

How activity back stack operation works explain with diagram?

The task comes to the foreground and resumes the activity at the top of the stack if the user subsequently continues it by clicking the launcher icon that started it in the first place. The current activity is removed from the stack and deleted when the user hits the Back button.

What is taskaffinity?

Task affinity lets you define which task an activity belongs to. By default, an activity has the same task affinity as its root activity. With task affinity, we can now separate activities into different tasks.

What is intent flag_ activity_ new_ task?

FLAG_ACTIVITY_NEW_TASK is equivalent to launchMode=singleTask and in there I read. 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.


1 Answers

If your Activity A has destroyed by Android OS itself then there are no way to track.

Some people has suggested to track that Activity A by listning event in onDestroy method BUT if your Activity killed by system OS then note here it wont call those method .

like image 128
dharam Avatar answered Sep 27 '22 18:09

dharam