[英]Dismiss dialog in AsyncTask from MainActivity
In the MainActivity
, I have an AsyncTask
in which a ProgressDialog
is displayed, starting in onPreExecute()
. 在
MainActivity
,我有一个AsyncTask
,其中显示了一个ProgressDialog
,从onPreExecute()
。 If the processing in the doInBackground()
crashes, then I get this exception: 如果
doInBackground()
的处理崩溃,则出现此异常:
Activity us.nm.state.mmd.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView 活动us.nm.state.mmd.MainActivity已泄漏窗口com.android.internal.policy.impl.PhoneWindow $ DecorView
I believe it is occurring because the dialog is still being displayed while the activity is going down. 我相信这是因为活动停止时仍在显示对话框。 The examples of AsyncTask/Progress dialog I see all have the dialog within the
AsyncTask
class. 我看到的所有AsyncTask / Progress对话框示例都在
AsyncTask
类中包含该对话框。 I would like to try dismissing the dialog in the Activity.onPause() method (as below), but since the dialog in declared in AsyncTask
class, MainActivity
does have access to it. 我想尝试关闭Activity.onPause()方法中的对话框(如下所示),但是由于对话框是在
AsyncTask
类中声明的,因此MainActivity
确实可以访问它。
I moved the instantiation of the dialog to MainActivity
. 我将对话框的实例移动到
MainActivity
。 So onPause()
does get the called and the call to dismiss()
happens, but this is long after the message "Unfortunately MyApp has stopped" appears on the device. 因此
onPause()
确实得到了调用,并且对dismiss()
的调用发生了,但这很久之后设备上出现消息“不幸的是MyApp已停止”。
How do I do this? 我该怎么做呢?
public class MainActivity extends AppCompatActivity implements ActionBar.TabListener {
private ProgressDialog nDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
nDialog = new ProgressDialog(mainActivity);
nDialog.setMessage(resources.getString(R.string.loading));
nDialog.setTitle(resources.getString(R.string.loading));
nDialog.setIndeterminate(false);
nDialog.setCancelable(false);
String url = "http://----------------------------------------";
Fragment myFragment = mTabsPageAdapter.getItem(TabsPagerAdapter.MY_TAB);
new LoadAndStoreDataTask((OnLoadAndStoreCompleteListener)permitsFragment, nDialog).execute(url);
}
@Override
public void onPause(){
super.onPause();
if(nDialog != null)
nDialog.dismiss();
}
}
private class LoadAndStoreDataTask extends AsyncTask <String, Integer, String> {
private ProgressDialog nDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
Resources resources = getResources();
nDialog.show();
}
protected String doInBackground(String... urls) {
....
}
@Override
protected void onPostExecute(String result) {
if (nDialog != null) {
nDialog.dismiss();
}
}
@Override
protected void onCancelled() {
if (nDialog != null) {
nDialog.dismiss();
}
}
change 更改
private class LoadAndStoreDataTask extends AsyncTask <String, Integer, String> {
private ProgressDialog nDialog;
to 至
private class LoadAndStoreDataTask extends AsyncTask <String, Integer, String> {
so the task will used the MainActivity nDialog field instead of the one in the LoadAndStoreDataTask class 因此该任务将使用MainActivity nDialog字段代替LoadAndStoreDataTask类中的一个字段
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.