Use Case Android: listen to connection state changes within a LiveData object

Julien Bouffard
1 min readJan 10, 2020

It’s a common use case in an android app to warn your app user he’s not connected anymore or refresh data when the network is back.

With LiveData, it’s become very easy to do so. Thanks to the broadcast receiver for connectivity change, we are pinged during a change and can determine exactly all the info needed from the network with the ConnectivityManager.

According to the google doc, it’s more resource friendly to register a broadcast receiver when needed. That’s why with a live data object, if no one is observing it, the broadcast receiver is not registered. On the contrary, as soon as the live data object is not active anymore, we unregister the broadcast receiver. We can apply this network status listener only in views where it matters for the app users.

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

No responses yet