Skip to content

Instantly share code, notes, and snippets.

@vbuberen
Created November 3, 2019 12:10
Show Gist options
  • Save vbuberen/678831df1b7d3d1951b8ce30fd9bedd9 to your computer and use it in GitHub Desktop.
Save vbuberen/678831df1b7d3d1951b8ce30fd9bedd9 to your computer and use it in GitHub Desktop.
Extension function, which helps to forget about `ActivityNotFoundException` in your Android apps.
// It is better to use dialogs with detailed description of error, but for clarity of this gist I decided to use Toasts here
fun Context.startIntentOrShowError(intent: Intent,
errorDialogDescription: String,
errorDialogTitle: String = "No app found!") {
if (intent.resolveActivity(this.packageManager) != null) {
when (intent.resolveActivityInfo(this.packageManager, intent.flags).exported) {
true -> startActivity(intent)
else -> Toast.makeText(this, errorDialogTitle, Toast.LENGHT_SHORT).show()
}
} else {
Toast.makeText(this, errorDialogTitle, Toast.LENGHT_SHORT).show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment