Hassan
Hassan

Reputation: 417

Retrofit in android Activity lifecycle to keep data fresh

My retrofit interface method is returning rx Single with the response

@GET("/data")
Single<Response<List<Foo>>> getData();

and in my Activity onStart() method i call getData() to populate the data and until that am showing a loading Progress it will dismiss on success or fail

   getData().observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .doOnSubscribe(() -> showProgress("loading"))
            .doOnSuccess(listResponse -> hideProgress())
            .subscribe(new SingleSubscriber<Response<List<Foo>>>() {
                @Override
                public void onSuccess(Response<List<Cat>> response) {
                    if (response.isSuccessful()) setItems(response.body());
                }

                @Override
                public void onError(Throwable error) {
                    hideProgress(error.getMessage());
                }
            })

the first time i start the Activity all goes well the Progress will show and on success or fail it dissmised

The problem is
whenever i start a new Activity via startActivity() and then back to my Activity onStart() is called again and my Retrofit call gets executed again (i did that to keep my data fresh every time i enter my Activity)
but what happens is the Progress is shown till ever, no Success, no Fail happen, i even logged the request i am making on the server and there is no request being requested "at my second time not the first time the Activity opened"

what could the problem be is it in the Retrofit or Rxjava
is there any approaches to do the fresh data thing every time the activity is started or poped from the stack

Are we till ever doomed with the Android activity lifecycle curse...

Upvotes: 3

Views: 954

Answers (2)

Hassan
Hassan

Reputation: 417

My sixth sense told me that it is a silly mistake or a misunderstand
i was using a CompositeSubscription to add all my calls to it and unsubscribe all of them safely in onStop() by calling CompositeSubscription.unsubscribe() to prevent resource leaks
what was happening is when i return to my activity i was adding the same subscription to the composite and it was unsubscribed immediately

the docs says :

public void unsubscribe()

Unsubscribes itself and all inner subscriptions.

After call of this method, new Subscriptions added to CompositeSubscription >will be unsubscribed immediately.

and i resolve the problem by calling clear() instead

public void clear()

Unsubscribes any subscriptions that are currently part of this >CompositeSubscription and remove them from the CompositeSubscription so that >the CompositeSubscription is empty and able to manage new subscriptions.

then you are able to add the same subscription again and being called as it normally behave

24 hours lost for that. -_- .

Upvotes: 1

Muhammad Saad Rafique
Muhammad Saad Rafique

Reputation: 3189

You are not stopping your activity when you return to your activity you need to override your onResume() method. Instead of onCreate() or onStart() call your getData method in onResume(). This will do the job.

Upvotes: 0

Related Questions