Android Intents: Difference between revisions
Jump to navigation
Jump to search
Line 28: | Line 28: | ||
intent.type = "text/plain" | intent.type = "text/plain" | ||
startActivity(intent) | startActivity(intent) | ||
</syntaxhighlight> | |||
==Implicit With Choice== | |||
Android looks at the action, and prompts the user for all app which handle this.The user can make their choice a default however we can override this and force a choice. Notice we should always check for a valid intent or the app will crash | |||
<syntaxhighlight lang="kotlin"> | |||
val chooser = Intent.createChooser)myIntent, title) | |||
if(intent.resolveActivity(packageManager) !=null) { | |||
startActivity(chooser) | |||
} else { | |||
Log.d(...) | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 02:26, 27 January 2021
Introduction
Intents
There are two types of intents
- Explicit
- Implicit
Explicit
We can start an explicit intent with
val intent = Intent(this.MyActivityClass::class.java_
startActivity(intent)
Implicit
No destination intent is defined. The user will be prompted for which application to use. Not the use of the apply operator.
val intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT,"Hello World")
type = "text/plain"
}
startActivity(intent)
Quite nice compared with the code without the apply.
val intent = Intent()
intent.action = Intent.ACTION_SEND
intent.putExtra(Intent.EXTRA_TEXT,"Hello World")
intent.type = "text/plain"
startActivity(intent)
Implicit With Choice
Android looks at the action, and prompts the user for all app which handle this.The user can make their choice a default however we can override this and force a choice. Notice we should always check for a valid intent or the app will crash
val chooser = Intent.createChooser)myIntent, title)
if(intent.resolveActivity(packageManager) !=null) {
startActivity(chooser)
} else {
Log.d(...)
}