Android: raise AlertDialog from background thread
In my activity there's some stuff going on in a background thread, which gets started in Activity_1. The processing of the background thread ta开发者_高级运维kes a while and I want to notify the user when it's completed via an AlertDialog
. However, the user might have changed to Activity_2 or Activity_3 in the meantime and I would like to pop up the AlertDialog always in the current Activity.
Any idea how to realize this?
I ended up doing something like this in my background thread. It works, but not sure if it is a "good" solution.
Looper.prepare();
mActivity.showDialogAlertDefault();
Looper.loop();
Looper.myLooper().quit();
This probably means the right way is to use a Service, instead of a plain background thread. Your background work will be more resilient to being paused or shut down, and you don't have to worry about making an AsyncTask that is constantly keeping track of a different Activity parent.
精彩评论