I take you never considered caching data and/or decoupling the data retrieval/processing from your UI lifecycle? That is a solution. Shoehorning fragments and misusing their lifecycle events is not. In case you haven’t noticed, you’re at best subscribing and unsubscribing at the host activity’s onCreate() and onDestroy(). At worst, as Eugenio Marletti points out, you’re doing it multiple times when there is a configuration change, or a fragment is recycled, etc. If you have a 1:1 mapping between subscribing and performing a network request, the problem won’t be solved by juggling the lifecycle of a fragment. You should subscribe to a data source that only performs request when it needs to. At that point you can freely subscribe and unsubscribe whenever, it makes no difference. See https://github.com/Dorvaryn/unidirectionalDataFlow and the associated talk here https://de.droidcon.com/en/sessions/let-it-flow-unidirectional-data-flow-architecture-android for an example of architecture that decouples things, but you can do simpler things than that if you want and still address the problem.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Sebastiano Poggi — seb@androiddev.social
Sebastiano Poggi — seb@androiddev.social

Written by Sebastiano Poggi — seb@androiddev.social

"It depends" 🤷‍♂️ - UXE on Android Studio at Google. A geek 🤓 who has a serious thing for good design ✨ and for emojis 🤟 Personal opinions only 😁

Responses (1)

Write a response