
Editorial illustration.
Originally published 26 June 2023; rewritten and reviewed on 15 October 2026.
An upload starts on Wi-Fi and the user walks outside. A connection change is now an application behaviour problem: should the operation retry, pause or explain that progress is uncertain? Logging “mobile data” is only the beginning.
Observe the network the app actually uses
The original example relied on older connectivity broadcasts and NetworkInfo. For a modern Android application, consult the network-state guidance and APIs available at your minimum supported level.
On API level 24 and later, a default-network callback is a useful starting point:
ConnectivityManager manager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
ConnectivityManager.NetworkCallback callback =
new ConnectivityManager.NetworkCallback() {
@Override
public void onCapabilitiesChanged(
Network network, NetworkCapabilities capabilities) {
boolean wifi = capabilities.hasTransport(
NetworkCapabilities.TRANSPORT_WIFI);
boolean cellular = capabilities.hasTransport(
NetworkCapabilities.TRANSPORT_CELLULAR);
// Pass state to the app's connectivity model.
}
};
manager.registerDefaultNetworkCallback(callback);
// Unregister this callback when its lifecycle owner stops observing.
This fragment belongs inside an appropriate lifecycle owner and requires the relevant imports and ACCESS_NETWORK_STATE manifest permission. Do not repeatedly register callbacks without unregistering them.
Separate transport from usable connectivity
Wi-Fi can lead to a captive portal, and a mobile connection can be temporarily unavailable. VPNs and multiple transports complicate an either-or label. Use network capabilities and the outcome of the actual operation rather than treating a transport name as proof that the internet is reachable.
Keep callback work short and move UI updates onto the appropriate thread. Preserve enough operation state to retry safely without duplicating a completed upload.
Design the transition the user sees
Show a state the application can justify: waiting, retrying, completed or failed. Test Wi-Fi loss, cellular restrictions and switching networks during a request. A well-handled transition protects the user’s work, not merely the accuracy of a network icon.