Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I track down why my activity gets finished?

Tags:

java

android

Context

I'm one of the developers working on a pretty large and very complicated project. Half of the project is written with JavaScript and the other half with Java, moreover about 30% of the base product comes to us pre-compiled, so we cannot exactly look inside or debug through it.

Problem

When our application comes back to foreground the activities stack gets destroyed and only the root activity is displayed. I cannot track down why.

Things I've tried so far

  1. Setting a breakpoint on every finish() call I could find, none of them gets called
  2. Setting android:excludeFromRecents="false" to every activity
  3. Removing android:launchMode="singleTop" from the AndroidManifest
  4. I've set a breakpoint in onDestroy() method for the second activity and I can see that "isFinishing()" method is executed to "True", so I know that it is not the OS that is destroying the activity, but there is no information (as far as I can see) which would pinpoint me to which class/property/method caused the method to be called. There is nothing usefully I can see in the stack trace. There is nothing useful I can see in the logs.
  5. I've also tried asking people around in my company, but no answer this far.

Question

Are there any strategies to find what caused a certain lifecycle method to be called?

Update 1

Thank you for the great questions and the help in the comments section. Here is the sequence diagram to explain better what is happening.

  1. Application is launched
  2. Activity A calls onCreate()
  3. Activity A calls onResume()
  4. I launch Activity B
  5. Activity A calls onUserLeaveHint()
  6. Activity A calls onPause()
  7. Activity B calls onCreate()
  8. Activity B calls onResume()
  9. Activity A calls onStop()
  10. I put the application into the background
  11. Activity B calls onUserLeaveHint()
  12. Activity B calls onStop()
  13. I return the application to foreground
  14. Activity B calls onDestroy()
  15. Activity A calls onRestart()
  16. Activity A calls onResume()

Update 2

Here are the screenshots of the memory usage

When I launch the application enter image description here

When I launch Activity B

enter image description here

When I send the application to the background

enter image description here

When I return to foreground

enter image description here

like image 766
Vlad Spreys Avatar asked Aug 23 '15 21:08

Vlad Spreys


People also ask

How do I know if my activity is finished?

Using activity. isFinishing() is the right one solution. it return true if activity is finished so before creating dialog check for the condition.

What is an activity in Android?

An activity provides the window in which the app draws its UI. This window typically fills the screen, but may be smaller than the screen and float on top of other windows. Generally, one activity implements one screen in an app.


2 Answers

And the solution is found!

Turns out that my root activity had the following property set:

android:clearTaskOnLaunch="true"

Here is a quote from the documentation:

android:clearTaskOnLaunch Whether or not all activities will be removed from the task, except for the root activity, whenever it is re-launched from the home screen — "true" if the task is always stripped down to its root activity, and "false" if not. The default value is "false". This attribute is meaningful only for activities that start a new task (the root activity); it's ignored for all other activities in the task. When the value is "true", every time users start the task again, they are brought to its root activity regardless of what they were last doing in the task and regardless of whether they used the Back or Home button to leave it. When the value is "false", the task may be cleared of activities in some situations (see the alwaysRetainTaskState attribute), but not always.

Suppose, for example, that someone launches activity P from the home screen, and from there goes to activity Q. The user next presses Home, and then returns to activity P. Normally, the user would see activity Q, since that is what they were last doing in P's task. However, if P set this flag to "true", all of the activities on top of it (Q in this case) were removed when the user pressed Home and the task went to the background. So the user sees only P when returning to the task.

If this attribute and allowTaskReparenting are both "true", any activities that can be re-parented are moved to the task they share an affinity with; the remaining activities are then dropped, as described above.

And here is a great article which helped me to find it: http://developer.android.com/guide/components/tasks-and-back-stack.html

like image 92
Vlad Spreys Avatar answered Oct 20 '22 00:10

Vlad Spreys


Not sure if this will work:

@Override
public void finish() {
   super.finish();
   Log.d("derp", "who is calling me?", new RuntimeException());
}
like image 37
Kevin Krumwiede Avatar answered Oct 19 '22 23:10

Kevin Krumwiede