简体   繁体   English

如何在按下后退按钮时停止异步任务执行

[英]How to stop async task executions when back button is pressed

Please i have an activity that contains async task method, This async task method continues to execute in background even when the user has moved from that page. 请我有一个包含异步任务方法的活动,即使用户已从该页面移动,此异步任务方法也会继续在后台执行。 Please is there a way to stop async task executions as soon as a user press on the back button or move from that page to another page. 只要用户按下后退按钮或从该页面移动到另一页面,就有办法停止异步任务执行。

Below is what i have tried but it isnt working 以下是我尝试过但它不起作用

private AsyncTask totalLikesAsync;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
    context = this;

   totalLikesAsync = new TotalLikes().execute(msgID);
}






@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case android.R.id.home:

            totalLikesAsync.cancel(true);
            this.finish();
            break;

        case R.id.share:
            new ShareImage().execute(msgImage);
        default:
            return super.onOptionsItemSelected(menuItem);
    }
    return true;
}





private class TotalLikes extends AsyncTask<String, String, JSONObject> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }



    @Override
    protected JSONObject doInBackground(String... msgIDs) {
        UserFunctions userFunction = new UserFunctions();

        String msgsID = msgIDs[0];
        JSONObject json=null;

        if(!isCancelled()){

            //This gets all the information unread from the server
            json = userFunction.TotalLikes(context, msgsID);
        }

        return json;
    }

    @Override
    protected void onPostExecute(JSONObject json) {
        /**
         * Checks for success message.
         **/
        if (json != null) {

            ...
        } else {

           ...
        }

}
}

UPDATE UPDATE

After testing all the answers below, they all allow the execution of doinbackground within the async task even though the back button is pressed. 在测试下面的所有答案后,即使按下后退按钮,它们都允许在异步任务中执行doinbackground I want a situation where the Background would be cancelled as soon as back button is pressed. 我想要一个按下后退按钮后取消背景的情况。

@Override
public void onBackPressed() {
    super.onBackPressed();
    if (totalLikesAsync != null && totalLikesAsync.getStatus().equals(AsyncTask.Status.RUNNING)) {
        totalLikesAsync.cancel(true);
    }
} 
@Override
public void onBackPressed() {
    super.onBackPressed();
    totalLikesAsync.cancel(true);
}
@Override
public void onBackPressed() {
TotalLikes.cancel(true);
// If you want to finish the activity you can use below code
// finish(); 
super.onBackPressed();
}

handle yourself the onBackPressed() method: 自己处理onBackPressed()方法:

@Override  
public void onBackPressed() 
  {
   if (totalLikesAsync != null)
   totalLikesAsync.cancel(true);

   super.onBackPressed();
  }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM