Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit and Activity Lifecycle

I want to make a REST Client with Android and I read about retrofit. When I do some requests with retrofit even if the activity is destroyed the callback is executed.

How can I stop receiving the response after the activity is being destroyed?

apiService.getDummieContent().enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {

    }
});
like image 446
user2897452 Avatar asked Jan 17 '17 15:01

user2897452


People also ask

What is activity and its lifecycle?

An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() . The system invokes each of these callbacks as an activity enters a new state.

What is the difference between onStop and onDestroy?

OnDestroy will be called directly from any call to finish() in onCreate, skipping over onStop. onDestroy can be left out after a kill when onStop returns. Starting with Honeycomb, an application is not in the killable state until its onStop() has returned; pre-honeycomb onPause was the killable state.

When onPause of activity lifecycle is called?

An activity can frequently transition in and out of the foreground—for example, onPause() is called when the device goes to sleep or when a dialog appears. Because this state can transition often, the code in these two methods should be fairly lightweight in order to avoid slow transitions that make the user wait.


2 Answers

You can declare global variable apiService

Call<ReponseBody> apiService;

and then call api method for instance here getDummieContent().Moreover you can manually check for the if call is cancelled in OnFaliure method. As in my case i was disabling swipeRefreshLayout which was getting null context on DestroyView because onFailure method was only called when view was destroyed, so with the help of this condition i always checked whether it is cancelled after on destroyed hence preventing exception.

apiService.getDummieContent().enqueue(new Callback<ResponseBody>() {

   @Override
   public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) 
   {

     // Do stuff here

   }

  @Override
  public void onFailure(Call<ResponseBody> call, Throwable t) {

  t.printStackTrace();

  if(call.isCanceled())
   {
      Log.e("isCanceled", " isCanceled");// check if it is cancelled on OnDestroy

   }

}

});

and in on destory onDestroy

@Override
public void onDestroy() {
    super.onDestroy();
    call.cancel();
}
like image 27
Karan sharma Avatar answered Sep 17 '22 13:09

Karan sharma


You can assign the request to a variable and cancel it on activity destroy:

Call<ReponseBody> call = apiService.getDummieContent();
call.enqueue(...);

@Override
protected void onDestroy() {
    super.onDestroy();
    call.cancel();
}
like image 91
Hristo Stoyanov Avatar answered Sep 18 '22 13:09

Hristo Stoyanov