|
![](/i/favi32.png) Pro Android with Kotlin@de android telegram Pro Android with Kotlin Developing Modern Mobile18
CHAPTER 3: Activities
When the called intent returns the way described earlier, the calling component needs to be
informed of that event. This is done asynchronously since the
startActivityForResult()
method immediately returns and does not wait for the called activity to finish. The way this event
gets caught nevertheless is by overriding the
onActivityResult()
method, as shown here:
override
fun onActivityResult(requestCode:Int, resultCode:Int,
data:Intent) {
// do something with 'requestCode' and 'resultCode'
// returned data is inside 'data'
}
requestCode
is whatever you set inside
startActivityForResult()
as
requestCode
, and
resultCode
is what you wrote as the first argument in
setResult()
in the called activity.
Caution
On some devices,
requestCode
has its most significant bit set to 1, no matter
what was set before. To be on the safe side, you can use the Kotlin construct inside
onActivityResult()
as follows:
val requestCodeFixed = requestCode and 0xFFFF
Intent Filters
Intents are objects to tell Android that something needs to be done, and they can be
explicit
by exactly specifying which component needs to be called or
implicit
if we don’t specify the
called component but let Android decide which app and which component can answer the
request. In case there is some ambiguity and Android cannot decide which component to
call for implicit intents, Android will ask the user.
For implicit intents to work, a possible intent receiver needs to declare which intents it is able
to receive. For example, an activity might be able to show the contents of a text file, and a
caller saying “I need an activity that can show me text files” possibly connects to exactly this
activity. Now the way the intent receiver declares its ability to answer intent requests is by
specifying one or more
intent filters
in its app’s
AndroidManifest.xml
file. The syntax of such
a declaration is as follows:
android:label="string resource"
android:priority="integer" >
...
Here,
icon
points to a drawable resource ID for an icon, and
label
points to a string
resource ID for a label. If unspecified, the icon or label from the parent element will be used.
The
priority
attribute is a number between -999 and 999 and for intents specifies its ability
to handle such intent request, and for receivers specifies the execution order for several
receivers. Higher priorities come before lower priorities.
|
|
|