Android - Stop AsyncTask when back button is pressed and return to previous Activity

前端 未结 4 1922
再見小時候
再見小時候 2021-02-09 04:22

I\'ve an AsyncTask and I want it to stip execution when back button is pressed. I also want the app to return to the previous displayed Activity. It seems I\'ve managed in stop

4条回答
  •  南方客
    南方客 (楼主)
    2021-02-09 05:14

    pd.setCancelable(true);
        pd.setOnCancelListener(cancelListener);
        bCancelled=false;
    
     pd is your progressdialog box
    

    and now use cancelListner

        OnCancelListener cancelListener=new OnCancelListener(){
        @Override
        public void onCancel(DialogInterface arg0){
            bCancelled=true;
            finish();
        }
    };
    

提交回复
热议问题