Last active
October 7, 2020 09:19
-
-
Save BuiVanNam/00e53c5cadfdce657d756b639fc497a6 to your computer and use it in GitHub Desktop.
Demo Lifecycle-Aware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
NetworkConnectLifecycle(this, lifecycle).run { | |
networkChange = { handleNetworkChange(it) } | |
} | |
} | |
private fun handleNetworkChange(connected: Boolean) { | |
runOnUiThread { | |
if (connected) { | |
Snackbar.make(content_main, R.string.network_connect, Snackbar.LENGTH_LONG).show() | |
status_network.run { | |
setText(R.string.status_network_connect) | |
setCompoundDrawablesWithIntrinsicBounds( | |
ContextCompat.getDrawable(this@MainActivity, R.drawable.ic_emotions_happy), | |
null, | |
null, | |
null | |
) | |
} | |
} else { | |
Snackbar.make(content_main, R.string.network_disconnect, Snackbar.LENGTH_LONG) | |
.show() | |
status_network.run { | |
setText(R.string.status_network_disconnect) | |
setCompoundDrawablesWithIntrinsicBounds( | |
ContextCompat.getDrawable(this@MainActivity, R.drawable.ic_emotions_sad), | |
null, | |
null, | |
null | |
) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment