0

Specifically, if I have a method running in my Activity class, then when the phone goes to sleep (CPU off), will the method run to completion before onPause() is called?

class MyActivity extends Activity {
    ...
    public void doSomeWork() {
        ...
        ... // <-- Device goes to sleep at this point in time
        ...
        return; // Do we get here before onPause() is called?
    }
    ...
    @Override
    public void onPause() {
        ...
    }
    ...
}

In addition to Activities, will a running thread (perhaps launched by an Activity class) be interrupted at whatever line of code it's at when the device goes to sleep? I see contradicting answers in this post: Does a Thread launched by Activity continue running once the device goes to sleep?

Going further, I understand that the Activity's onResume() will be invoked when the device wakes up; if doSomeWork() above or the aforementioned thread was indeed interrupted midway when going to sleep, will the remaining code resume?

Thanks in advance for any insight.

Community
  • 1
  • 1
Phillip
  • 5,366
  • 10
  • 43
  • 62

2 Answers2

0

It's considered to be a good Android practice not to run long tasks on the main UI thread, therefore your doSomeWork() is supposed to finish within a few hundred milliseconds and reach return statement before onPause() is called.

Regarding doSomeWork() interrupted midway -- the only possibility of this to happen is when your application shows the dreaded ANR dialog and force closes, so you should not be concerned about restarting it halfway through.

lenik
  • 23,228
  • 4
  • 34
  • 43
-1

Here when you move out of the activity or a pop up comes to your activity like a phone call appears or you rotate your screen on Pause is called. The link provided by you talks about AsyncTask. AsyncTask is a thread that runs background and updates UI after fetching some data, During the process if the user rotates the phone Android kills the activity and recreates if the thread is linked to the activity then your activity will not be killed but leaks out.

If you need more details on AsyncTask & activity you can read this Is AsyncTask really conceptually flawed or am I just missing something?

Community
  • 1
  • 1
  • Chaitanya -- Actually, the link specifically mentions it's _not_ referring to AsyncTask. Please read it carefully. – Phillip Jun 27 '12 at 18:12
  • If your question is onPause() is called before doSomething() method's return statement, it depends on your device capacity and sensitivity of your touch. – Chaitanya GOPIREDDY Jun 27 '12 at 18:22