0

Actually the scenario is a bit more complex than described it the title.

The situation is the following:

  • Activity A starts Activity B.

  • Activity A must not be destroyed when I start Activity B because I need the user to be able to navigate back to A.

  • When the user presses the HOME button the user opens the Recent Apps window and switches from my app to another app. At this stage both A and B are STOPPED.

  • When the user user opens the Recent Apps window and switches back for the other app to my app: Activity B is RESTARTED (activity A is not restarted yet)

  • Now on Activity B there is a button to close the entire app, closing both B and A, and it does close both activities using this approach: https://stackoverflow.com/a/11509279/1815311

  • THE PROBLEM IS THAT WHEN ACTIVITY B TRIES TO CLOSE BOTH B AND A, IN THE DESCRIBED SCENARIO, ACTIVITY A SOMETIMES IS NULL !!

How do I cope with such a scenario?

Community
  • 1
  • 1
Lisa Anne
  • 4,482
  • 17
  • 83
  • 157
  • 2
    Approach mentioned in http://stackoverflow.com/a/11509279/1815311 is **very-very** bad and causes activity leaks and "null pointer exceptions". Android doesn't work this way. I would strongly recommend rethinking your app navigation. This is a good start - http://developer.android.com/training/design-navigation/index.html – Pavel Dudka Jun 10 '14 at 18:25
  • 1
    Have you considered using _startActivityForResult_ to start Activity B. And return such a code back to Activity A it knows to call _finish()_ on itself? – harism Jun 10 '14 at 18:25

1 Answers1

0

1 solution is - before finishing the activity B, store some variable with value 1 using sharedpreferences, finish activity B but not A. system will resume activity A. override onResume() function in activity A and get the variable from shared preferences, if it states 1 then store 0 there, and finish() activity A.

2 solution is overriding onresultactivity - see here

How to kill an application with all its activities?

Community
  • 1
  • 1
Ronn Wilder
  • 1,228
  • 9
  • 13