0

Is there any way to call method when main thread of application (UI thread) is finished ? I read about onTerminate() method in Application class, but there is written :

This method is for use in emulated process environments. It will never be called on a production Android device, where processes are removed by simply killing them; no user code (including this callback) is executed when doing so.

Are any alternatives ?

TN888
  • 7,659
  • 9
  • 48
  • 84
  • http://stackoverflow.com/q/3511741 – Robert Harvey Aug 01 '13 at 19:04
  • 1
    I've seen [this](http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29) but never tried in Android tbh. You might give it a try. – m0skit0 Aug 01 '13 at 19:06
  • http://stackoverflow.com/questions/13785720 – Robert Harvey Aug 01 '13 at 19:06
  • @m0skit0 Although I'm not discouraging anyone from trying, I highly doubt that'll work. It's a reasonable thought, but as the quote in the original post notes, apps are often stopped by the system by simply killing the process. See the [Android docs for the method](http://developer.android.com/reference/java/lang/Runtime.html#addShutdownHook%28java.lang.Thread%29): "Note that on Android, the application lifecycle does not include VM termination, so calling this method will not ensure that your code is run." – kabuko Aug 01 '13 at 19:31

1 Answers1

0

Interesting question. You may get some useful answers by posting more about what your app is trying to do. Maybe there's a better way to write your app, or maybe you actually don't need to worry about onTerminate().

As a note, a UI thread "finishing" is different from an Application "terminating". The main thread doesn't "finish"; instead, the system kills its parent process. A component (such as an Activity) running on the UI thread finishes, but the thread itself remains as long as process remains (AFAIK).

The Application object is singularly unimportant in Android. An app is much more a collection of interacting components. The Application object doesn't do very much.

Joe Malin
  • 8,621
  • 1
  • 23
  • 18