I use activity A to start background download using AsyncTask, when download finished, AsyncTask.postExecute will popup a AlertDialog. However, during download, I switch to another Activity B or any other activities. The result is that the AlertDialog can’t show out. AlertDialog uses activity context(Here should be A). So, how can I show out the AlertDialog?
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(context.getString(R.string.install));
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//onSure();
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//onCancle();
}
});
builder.create().show();
If it is a long running task that can’t complete before
Activity Acompletes then I would suggest using an IntentService. But it depends on what you need. Doing this will allow it to finish in the background and not hold up theUI. There are probably other options but this sounds like the best from what I know of your situation. Other than that, you can wait to start the next activity and start it with a button in the dialog once theAsyncTaskfinishes, if that is an option