4

I have an app with 4 activities int the sequence say A->B->C->D and a service S that is started by A when the app is started. Now on a particular condition this service triggers a notification which again should start activity C withing my app and with the arguments that C usually needs from B. Also to function properly, C needs to use a static variable from the activity A. That was the setup.

Now what happens is when the notification is triggered from the service S and I click on it, activities A and B are automatically destroyed resulting in a force close(Since my activity C depends on a static variable in A). Why does this happen. I have used a TaskStackBuilder to place C's parent activity(i.e B) onto the stack so that normal back behavior takes me back to activity B.

Can anyone tell me the reason of this behavior. I do not want to run my Activity C as an interdependent task. I want it to be the same instance as that already in the app. Please help me understand the problem here.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ricky
  • 81
  • 11
  • Add Some Code so that some one be able to find the problem – TNR Jan 17 '13 at 09:53
  • too messed up.! not getting the real problem. – Sahil Mahajan Mj Jan 17 '13 at 09:55
  • @SahilMahajanMj when I click the notification to start activity C, it is destroying my Acivites A and B. That is the problem – Ricky Jan 17 '13 at 10:02
  • @SahilMahajanMj In the normal workflow activity B starts the activity C – Ricky Jan 17 '13 at 10:03
  • @Ricky What flags are you using to start activity C from B. Is it something like **FLAG_ACTIVITY_CLEAR_TASK**.? what do you exactly mean by _Destroying_.? Does it means force close or just activity termination.? – Sahil Mahajan Mj Jan 17 '13 at 11:25
  • @SahilMahajanMj I did not use any flag as such, I meant activity termination, onDestroy for activity B is being called. The normal flow of the application is as such A starts B and then B starts C. Now depending upon some error checking an Android Notification can also start C, when this is happening activity B and A's onDestroy methods are being called. – Ricky Jan 17 '13 at 13:59
  • You need to check the intent used to show **Notification**. I doubt it might be responsible for it, something like **FLAG_ACTIVITY_CLEAR_TOP** or something that is destroying the previous activities. But it is still impossible to tell the error without looking at the code. – Sahil Mahajan Mj Jan 17 '13 at 14:03
  • @SahilMahajanMj I figured out that this is because the Activity Lifecycle behaves differenty from ICS onwards I have placed a new question with sample code here http://stackoverflow.com/questions/14441951/activity-lifecycle-behaves-differently-jellybean-onwards . Please let me know – Ricky Jan 21 '13 at 15:31
  • okay, let me have a look at it. But I dont think thid behaviour might be due to different android versions. – Sahil Mahajan Mj Jan 22 '13 at 05:41
  • @SahilMahajanMj.. Ok, please let me know what you think, as I could find only that one thing to be differing in two executions of the same application. I also tried it with two versions of the emulator and found the same behavior, this is exactly what puzzled me, eagerly waiting for your observations. – Ricky Jan 22 '13 at 09:05

2 Answers2

2
  1. Activity should be independent. It is dangerous that activity C needs to use a static variable from the activity A. Although you create activities in order like A->B->C->D, Android may destroy A/B/C/D when your app is in background, and when user returns to your app, only activity D is recreated.

  2. I encountered the same problem as you once, Starting an Activity from Notification destroys Parent Acitivities. This is because I used TaskStackBuilder. And even after I stop to use TaskStackBuilder, the problem remains. Only after I uninstall the app, it works as expected. Check the comments for https://stackoverflow.com/a/28268331/1198666

Community
  • 1
  • 1
hhwithgroups
  • 291
  • 3
  • 4
0

This is happening because the Activity Lifecycle behaves differenty from ICS onwards. This fact answers this question, however I am yet to find out why this is happening. If someone wants to see the code and verify this behavior for themselves, HERE

Community
  • 1
  • 1
Ricky
  • 81
  • 11