Question

Anywhere I've looked for this topic says (resuming): "call finish() on your activity and if you have any services running, call stopService() on them".

So that I did, and as it seemed to work, I stopped worrying. Today, debugging a part of my code, I needed to add a Log line into a non-ending thread's method which I want to be executed all the app's life, basically it's something like this:

final Thread buf_liberator = new Thread(
  new Runnable() {
    public void run() {
      while (true) {
        methodCall();
        SystemClock.sleep(9000);
      }
    }
  } 
);
buf_liberator.setPriority(7);
buf_liberator.start();

In methodCall() I put the Log line, and for my surprise, after I closed my app pushing the Exit button which calls finish() and stops a service I've started, I still see the Log message every 9 seconds indefinitely in the LogCat.

So the question(s) is(are): This thread is not a service, why it keeps running? Am I doing something wrong? Is this an expected behavior? Is there a real method to destroy everything the app had in memory?

Thanks!

Was it helpful?

Solution

So the question(s) is(are): This thread is not a service, why it keeps running?

Because your process is still running. Even when you finish() an Activity or stop() a Service, your application process may keep running.

Am I doing something wrong?

Besides creating a never-ending thread? No.

Is this an expected behavior?

Yes. The Android system keeps process around as a caching mechanism to speed up resuming previously used apps. The system will eventually kill your process (and the running thread) as it deems necessary. Do not count on your thread not ending, because it will, eventually

Is there a real method to destroy everything the app had in memory?

There are ways, like System.exit(), but it's generally advised that you let the system work as intended.

Further Reading: http://developer.android.com/guide/components/processes-and-threads.html

OTHER TIPS

You don't need to worry about this. There is no such thing as "exit application" in Android. When application is launched, Android spawns a process where your application "lives". When your application starts an Android component (like an Activity or a Service), then Android knows your application is active, and it will try to keep it in memory then. If you close all activities and all services, your application's importance gets low. Since now, if Android needs more memory, it will kill the process hosting your application at any time.

In your case, you closed all components, but kept a thread running. It has continued to run in background because Android didn't need more memory and your app stayed "cached" for possible future use. If you had started a memory consuming game then your app would have been killed. So you cannot rely on the thread running outside of an active component (like a service) as it can be killed at any time.

If you really want to exit the app, you can try to call standard Java function System.exit(0). This is not necessarily needed, but you can use it if you want to.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top