1

I'm using rxAndroid with an MVVM architecture. In my Activity, I need to bind my streams then fetch data from the server, which will eventually call the downstream.

My reasoning is the following:
- I need to unsubscribe my streams in onStop() (onDestroy can cause memory leaks).
-> therefore I need to subscribe to them in onStart() (otherwise coming back from background doesn't recreate the stream).
-> therefore I need to fetch my data in or after onStart(), because the stream has to be bound before I can start calling the upstream.

But in this case, every time I come back to the app after background, it's going to call the fetch method, which is not a behavior I want. Ideally I'd like to call the fetch method once, for example in onCreate().

How can I nicely deal with this problem? I've tried finding solutions on SO and other websites but no luck.

1 Answers1

0

So what you are saying is you ONLY want the stream recreated if it hadn't completed yet when the Activity was destroyed and restarted? In this case consider a BehaviorSubject.

It would live outside of the context of the Activity (be sure not to hold a reference to any Activity in the subject, a weak reference would be okay.)

It will deliver the last fetched result when you reconnect to it from the new activity.

Nathan Schwermann
  • 31,285
  • 16
  • 80
  • 91