1

I'm writting an android app that instantiates a worker thread to update UI every second.

When I press "home" button, the thread still running, and when I restart the app from home, everything works fine: the UI is still updating every second.

But, when I press "back" button, and then restart the app from home, the app didn't get the UI updates sent from worker thread.

What can I do to get this thread reference again?

I need to do an app that works like a chronometer. If the user leaves the app without stop it, the work must continue. Is thread a good choice? Can I solve this using AsyncTask or Service?

Note: pressing "back" button, the onDestroy() method is executed. When I press "home" button, onDestroy() isn't executed.

(sorry for my bad english)

1 Answer 1

0

You should need a Boolean variable for handling UI update from background thread. by this variable you can control Thread when app is going distory and paused by pressing back and Home Key. change your code as:

boolean threadstatus=false;
@Override
         public void run(){
          try
          {

           while(threadstatus)
           {
             ///your code...
           }
         }
protected void onResume() {
        super.onResume();
        threadstatus=true;
    }
    @Override
    protected void onPause() {
        super.onPause();
       threadstatus=false;
    }
    @Override
    protected void onStop() {
        super.onStop();
        threadstatus=false;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        threadstatus=false;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.