Pro Android with Kotlin


Download 5.42 Mb.
Pdf ko'rish
bet31/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   27   28   29   30   31   32   33   34   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

18
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.



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   27   28   29   30   31   32   33   34   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling