1

We are using CLEAR_TOP and NEW_TASK flags to tidy up a back stack in Android when displaying certain views

For instance in an app that has ViewA, ViewB, ViewC and ViewD we are are always loading ViewC with CLEAR_TOP and NEW_TASK.

In the case of a BackStack of

  1. ViewA (Root)
  2. ViewB
  3. ViewC
  4. ViewD

Displaying ViewC from ViewD would cause ViewD to be destroyed. Is there anyway of ViewD knowing it has been destroyed because of CLEAR_TOP rather than simply because the user has gone away from the View?

Pat Long - Munkii Yebee
  • 3,592
  • 2
  • 34
  • 68
  • Why don't you just use singleTask for viewC. It clears activities above viewC. If you already have viewC in your stack and start it again, viewD will be destroyed and viewC's onNewIntent() method will be called. I know this is not answering your question but I am just saying :) When we come to your question, I don't think any other method is called except onDestroy when it is cleared from activity stack. So you can check your stack before you start viewC and after viewC's onNewIntent() method call. – mertsimsek Apr 27 '17 at 11:49
  • "So you can check your stack" are you suggesting I can look at the items in the stack, find the current ViewD before loading the new ViewC and tell it "you are about to be removed"? If I can do that that would be great but i don't think it is possible – Pat Long - Munkii Yebee Apr 27 '17 at 11:53
  • Actually you can do that by getting activity list. `getPackageManager().getPackageInfo("your.package", PackageManager.GET_ACTIVITIES).activities` will give you ActivityInfo array. So you can acces activity name by calling `activityArray[0].name` – mertsimsek Apr 27 '17 at 12:31
  • That just gives me all the Activities in the package, not all the activities in the current back stack. How can I use that list to "check the stack"? – Pat Long - Munkii Yebee Apr 27 '17 at 12:51
  • Oh yes, you are right:/ Maybe you can try to keep track your activities by using `ActivityLifecycleCallbacks`. Did you check this out? http://stackoverflow.com/questions/3667022/checking-if-an-android-application-is-running-in-the-background/13809991#13809991 – mertsimsek Apr 27 '17 at 13:01

1 Answers1

0

Unless someone comes up with a cleaner solution this is what I have done.

I am trapping OnBackPressed and setting a boolean saying BackKeyPressed. In OnDestroy I check if that flag has been set, if it has not I decide that the Activity is being destroyed because the back stack is being shortened with CLEAR_TOP.

I realise that OnDestroy and flag == false could also be because the app is being closed but the action I am taking whilst unneccesary in that situation is safe

What other scenarios could OnDestroy be called in?

Pat Long - Munkii Yebee
  • 3,592
  • 2
  • 34
  • 68