开发者

Android: Progress Dialog not dismissed when activity pauses

开发者 https://www.devze.com 2023-03-20 08:04 出处:网络
I\'m having a small problem handeling ProgressDialog and the suer hitting the home key. I create my ProgressDialog as follows:

I'm having a small problem handeling ProgressDialog and the suer hitting the home key.

I create my ProgressDialog as follows:

runOnUiThread(new Runnable() {
       开发者_StackOverflow     public void run() {
                progressDialog = ProgressDialog.show(this, "",this.getResources().getString( R.string.AProgressMessage), true);
            }
        });

and dismiss it when I finished downloading stuff of internet.

progressDialog.dismiss();

the problem is when a user hit the home key, sometimes the Thread that calls the dismiss is kille but hte dialog never gets dismissed... therefore when the app relaunches it gets stuck behind a ProgressDialog.

Any ideas ?


I know this thread is quite old, but I think my answer could help many people.

Activity class has an onPause method. So if you call the dialog from the same activity that is being paused, you can use the onPause method to dimiss the dialog.

Make the dialog variable global, and define the onPause method as follows:

@Override
public void onPause(){
    super.onPause();
    if(dialog!=null)
        dialog.dismiss();
}


Why would the Thread be killed?

If the Android system thinks that the memory is low, it will only kill whole processes and not individual threads. Are you sure you always call progressDialog.dismiss(), even if the thread stops because of an uncaught exception (for example)?

By the way, you should probably use AsyncTask instead of doing the thread management yourself, see here.

0

精彩评论

暂无评论...
验证码 换一张
取 消