Issue
I am using AspectJ to instrument the lifecycle of an Android Activity. Basically, I have pointcuts for onCreate(Bundle)
and onDestroy()
and advice them BEFORE the methods are executed.
So basically, I count how many active references to activities I have, like a registry. So if onCreate(Bundle)
is called, I basically do count++. If onDestroy()
is called I do count--. So if my count becomes 0, there is no activity left which can be considered being an application close event in my case.
Now I am using a Timer (already tried java.util.timer and android.os.Handler) that postpones the execution of a method to shut my system down. However, in some cases, the timer is started but never executes the method. It looks like Android just kills the entire process. It does also not matter if I use daemon threads. There is no crash, no exception, the whole thing just stops in the middle of a method. I also encountered this behaviour when I was debugging the application. So I just reached a breakpoint and boom, the process was gone.
The funny thing is that I tried also without the timer, placing a LogCat message directly before and after my method. There was no timer, no separate thread or whatever. And from time to time, not even the first logcat message is printed although I can confirm that the method was called. It seems like the app just stops without anything. I am actually a quite experienced programmer, so I now how the lifecycle works, how AspectJ works and how multithreading has to be applied correctly. But this confuses me a lot!
Maybe someone can help me out!
Solution
Okay, found out that what I want to achieve cannot be solved this way. Whenever onDestroy()
is called from the UI thread for the last Activity, the shutdown process for the thread is already initiated, which means that the thread's looper will quit shortly after the method is executed. In this case, all timers will stop as well as they depend on the looper as well. This is btw. also noted in the documentation and it doesn't matter whether you are using java.util.Timer
or android.os.Handler
.
Daemon threads won't help either. Android forces daemon threads to stop when there is no other thread left in the process. This is actually not a big deal because if the app and the UI thread are gone, a daemon thread that is not killed would result in a zombie thread.
Answered By - Florian
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.