You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A) Change nested ifs in all fragments to if-else-if ladder.
if (!ConnectionUtils.getConnectionInfo(context)) {
uiThread { networkDownHandler.onNetworkDownError(resources.getString(R.string.error_check_internet)) }
} else if (!ConnectionUtils.isReachableByTcp(Constants.HOST, Constants.PORT)) {
uiThread { networkDownHandler.onNetworkDownError(resources.getString(R.string.error_server_down)) }
} else {
// Do actual thing
}
B) Apart from having RETRY button in that non cancellable (.setCancelable(false)) dialog box which you would be displaying from MainActivity, you can automatically detect if internet is available again by doing this:-
Add new module to dagger (it'll make MainActivity.kt short)
@Module
class ConnectivityModule {
@Provides
@BingoApplicationScope
internal fun getConnectivityManager(context: Context) = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
@Provides
@BingoApplicationScope
fun getNetworkRequest() = NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).build()
}
Inject these 2 things in MainActivity:-
@Inject
lateinit var connectivityManager: ConnectivityManager
@Inject
lateinit var networkRequest: NetworkRequest
In MainActivity.kt in onResume() connectivityManager.registerNetworkCallback(networkRequest, networkCallback)
In MainActivity.kt in onPause() connectivityManager.unregisterNetworkCallback(networkCallback)
Create networkCallback object like this:-
networkCallback = object : ConnectivityManager.NetworkCallback() {
override fun onAvailable(network: Network) {
super.onAvailable(network)
runOnUiThread {
// Do something ....
}
}
override fun onLost(network: Network) {
super.onLost(network)
// Do something as network lost
}
}
The text was updated successfully, but these errors were encountered:
A) Change nested ifs in all fragments to if-else-if ladder.
B) Apart from having RETRY button in that non cancellable (
.setCancelable(false)
) dialog box which you would be displaying from MainActivity, you can automatically detect if internet is available again by doing this:-onResume()
connectivityManager.registerNetworkCallback(networkRequest, networkCallback)
onPause()
connectivityManager.unregisterNetworkCallback(networkCallback)
The text was updated successfully, but these errors were encountered: