Stop AsyncTask in Fragments when Back Button is pressed

前端 未结 5 1509
别跟我提以往
别跟我提以往 2021-01-17 01:17

i have an activities that host fragments. pressing a button goes from fragment A to fragment B through a FragmentTransaction and the added to the back stack. Now fragment B

5条回答
  •  一向
    一向 (楼主)
    2021-01-17 01:43

    Override the onStop() of your fragment and do:

    protected void onStop() {
        super.onStop();
    
        //check the state of the task
        if(task != null && task.getStatus() == Status.RUNNING)
            task.cancel(true);
    }
    

提交回复
热议问题