Created
November 3, 2019 12:10
-
-
Save vbuberen/678831df1b7d3d1951b8ce30fd9bedd9 to your computer and use it in GitHub Desktop.
Extension function, which helps to forget about `ActivityNotFoundException` in your Android apps.
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
// 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