Search code examples
androidasynchronousandroid-asynctaskandroid-async-httploopj

How to handle back press action in loopj/android-async-http


I am using this library loopj/android-async-http for performing network operation in android app. In the case of AsyncTask when you press back button while asynctask is running then you can cancel it onBackPressed. I want to know how this can be done in this library loopj/android-async-http.

In case of AsyncTask. We do like below

@Override
public void onBackPressed() {
    mMyAsyncTask.cancel(true);
}

Do I have to handle this or loopj/android-async-http handles this automatically without I need to worry for this

Thanks in advance.


Solution

  • I did in this way in the fragment & it is working fine for me.

    @Override
    public void onDestroy() {
        super.onDestroy();
        // true specifies that even if the request is active it will be cancelled.
        if (asyncHttpClient != null) asyncHttpClient.cancelRequests(getActivity(), true);
    }