简体   繁体   English

活动已销毁,但asynctask仍在运行

[英]Activity is destroyed but asynctask is still running

I am working on push notifications. 我正在处理推送通知。 The issue is when i am loading a form which is very huge and i recieve notification, before the form is completely loaded i try to click on the notification i get a blank screen after 5-6 seconds. 问题是当我正在加载一个非常庞大的表单并且收到通知时,在表单完全加载之前,我尝试单击通知,在5-6秒后出现空白屏幕。 The exception is due to dialog.dismiss. 例外是由于dialog.dismiss。

According to my understanding, while opening notification the current activity is destroyed a new activity is created. 根据我的理解,在打开通知时,当前活动被破坏了,同时创建了一个新活动。 while creating a new activity i am using asynctask to accomplish some other functionality in my app. 在创建新活动时,我正在使用asynctask来完成我的应用程序中的其他功能。 So when the current activity is destroyed, activity context is null but the asynctask is still running also progress dialog. 因此,当当前活动被销毁时,活动上下文为null,但asynctask仍在运行,并且还会显示进度对话框。 As soon as the activity is destroyed there is no window to show the dialog and hence window leaked exception. 一旦活动被销毁,将没有窗口显示对话框,因此窗口泄漏异常。

Can anybody help me to get me out of this issue.I also get the blank screen when app is idle for a long time then i open the notifications. 任何人都可以帮助我摆脱这个问题。当应用长时间处于空闲状态时,我也会出现空白屏幕,然后打开通知。

Is there a way to stop running the asynctask as soon as the activity is destroyed. 一旦活动被破坏,有没有办法停止运行asynctask。

My code is : 我的代码是:

public class MainActivity extends Activity { 公共类MainActivity扩展了Activity {

  private MyProgressDialog myProgressDialog;  
  public  LinearLayout mainPanel;  
  private VMobilet mobilet = null;  
  private String mobiletId;  
  private String formId ;

  @Override  
protected void onCreate(Bundle icicle) {  
    super.onCreate(icicle);   
    setContentView(mainPanel);   

    Intent i = getIntent();  
    mobiletId = i.getStringExtra("Mobilet Id");  
    formId = i.getStringExtra("Form ID");  
            VUiHelper.getInstance().setIsFinish(false);  
    myProgressDialog = MyProgressDialog.show(MainActivity.this,"","",true);  

    BackgroundTask backgroundTask = new BackgroundTask();  
    backgroundTask.execute(MainActivity.this);  
}  

    @Override  
protected void onPause() {  
    super.onPause();  
    if(VUiHelper.getInstance().isFinish())  
        {         
            this.finish();    
        }       
    else {  
         System.out.println("pausing mainactivity");  
    }  
}  

    @Override  
protected void onDestroy() {  
    super.onDestroy();  
    System.out.println("mainactivity ondestroy called");  
    if(mobilet != null)  
    mobilet.getForms().clear();  
    mobilet = null;  
    mainPanel = null;  
    VUiHelper.getInstance().clearControlCache();  
    VUiHelper.getInstance().MediaInput.clear();  
    System.gc();  
}  

    private class BackgroundTask extends AsyncTask<Context, String, Boolean> {  

    @Override  
    public void onPreExecute() {    
        super.onPreExecute();   
    }  

    @Override  
    protected Boolean doInBackground(Context... arg0) {  
        if (mobiletId != null) {  
            ** some logic **  
        }  
        return true;  
    }  

    @Override  
    public void onPostExecute(Boolean status){              
         super.onPostExecute(status);          
         myProgressDialog.dismiss();  //dismissing the progress dialogs    
         if (mobilet != null) {  
                      ** some logic  **  
         } else {  
              ** some logic **  
                 }  
     }  
     }      
}  
 }  

I tried printing the activity context in onDestroy() and it is null. 我尝试在onDestroy()中打印活动上下文,它为null。

You can stop running an Asynctask (as long as you have a reference to it). 您可以停止运行Asynctask(只要您对其有引用)。 You might want to do something like this 您可能想做这样的事情

when starting: 开始时:

Task ref = new Task();
ref.execute()

then in onPause() 然后在onPause()

if(ref != null)
     ref.cancel(true);

then inside your doInBackground() if you are doing something periodically (like downloading) have something like this: 然后在doInBackground()内部,如果您定期执行某项操作(例如下载),则应执行以下操作:

if(isCanceled())
{
    return;
}

The above snippets should allow you to gracefully exit the asynctask. 上面的代码片段应允许您正常退出asynctask。

Also in your onPause 也在您的onPause中

if(myProgressDialog != null)
{
    if(myProgressDialog.isShowing())
    {
         myProgressDialog.dismiss()
    }
    myProgressDialog = null
}

then in your onPostExecute 然后在你的onPostExecute

if(myProgressDialog != null)
{
    if(myProgressDialog.isShowing())
    {
        myProgessDialog.dismiss;
        myProgessDialog = null;
    }
}

the setting of myProgessDialog to null in onPuase should make it null in onPOstExecute() 在onPuase中将myProgessDialog设置为null应该在onPOstExecute()中将其设置为null

You can cancel an AsyncTask by calling cancel(Boolean) . 您可以通过调用cancel(Boolean)取消AsyncTask。 For more usage info ctrl-f for "Cancelling a task" on that page 有关该页面上“取消任务”的更多用法信息ctrl-f

I think the best solution to the problem is let your asyntask check whether there is still an application running. 我认为对该问题的最佳解决方案是让您的asyntask检查是否仍在运行应用程序。 If not simply exit. 如果不是,直接退出。 Set a flag in Activity.onPause and the let the asynctask check it before sending some output. 在Activity.onPause中设置一个标志,并让asynctask在发送一些输出之前检查它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当启动活动在仍在运行时停止/销毁时,AsyncTask会发生什么? - What happens to an AsyncTask when the launching activity is stopped/destroyed while it is still running? 当Activity在其AsyncTask运行时被销毁时,内存中会发生什么? - What happens in memory when the Activity is destroyed while its AsyncTask is running? 使用ResultReceiver,如果活动被破坏并且Intent服务仍在运行,会发生什么情况 - Using ResultReceiver what happen if activity is destroyed and intent service is still running Activity.finish()上的AsyncTask仍然在后台运行会发生什么? - What happens on Activity.finish() with AsyncTask still running in background? 活动被销毁时AsyncTask的状态 - state of AsyncTask when the activity gets destroyed 与被破坏的活动相关联的垃圾收集器和AsyncTask - garbage collector and AsyncTask associated with the destroyed activity 在销毁和重新创建活动时,处理程序仍在旧的,销毁的活动实例中运行 - When destroying and recreating activity, handler is still running in old, destroyed activity instance 活动销毁后运行Bindservice - Running Bindservice after the activity is destroyed 活动被破坏后,对象仍在内存中 - Objects are still in memory after activity is destroyed AsyncTask 应用程序仍然运行缓慢 - AsyncTask app still running slowly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM