Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1
Android
12-2
Intents
Part2
InterProcessCommunicationUsingBundles
VictorMatos
ClevelandStateUniversity
Notesarebasedon:
AndroidDevelopers
http://developer.android.com/index.html
12.Android Intents Part2
Intents
AndroidIntents
An activity usually presents a single visual user interface from which a number of Anactivity usuallypresentsasinglevisualuserinterfacefromwhichanumberof
actionscouldbeperformed.
Movingfromoneactivitytoanotherisaccomplishedbyhavingthecurrent
activitystartthenextonethroughsocalledintents.
Intent
{action+data}
Activity1
22
{action data}
requestCode
requestResult
[optionaldata ]
Activity1
startActivityForResult
onActivityResult()
Activity2
onResult()
10/18/2011
2
12.Android Intents Part2
Intents
AndroidBundles
MostprogramminglanguagessupportthenotionofIPC methodcalling with
argumentsflowingbirectionallyfromthecallertotheinvokedmethod.
Inandroidthecallingactivityissuesaninvocationtoanotheractivityusingan
Intent object.
NotablyinAndroid,thecallerdoesnotstopwaitingfor
the called activity to return results Instead a listening
33
thecalledactivitytoreturnresults.Insteadalistening
method[onActivityResult(...)]shouldbeused.
IPCInterProcessCommunication
12.Android Intents Part2
Intents
AndroidBundles
NormallytheIPCexpressionsactualparameterlist,and
formalparameterlist areusedtodesignatedthesignatureof
particpatingarguments,andthecurrentlysupplieddata.
Insteadofusingthetraditionalformal/actualparameterlists,
AndroidreliesontheconceptofIntentstoestablishInterprocess
44
communication.
Intentsoptionallycarryanamedactuallistorbundle fordata
exchange.
10/18/2011
3
12.Android Intents Part2
Intents
AndroidBundles
TheAndroidBundle containerisasimplemechanismusedtopassdatabetween
activities.
ABundle isatypesafecollectionof<name,value> pairs.
ThereisasetofputXXX andgetXXX methodstostoreandretrieve(singleand
array)valuesofprimitivedatatypesfrom/tothebundles.Forexample
55
Bundle myBundle = new Bundle();
myBundle.putDouble ("var1", 3.1415);
...
Double v1 = myBundle.getDouble("var1");
12.Android Intents Part2
Intents
AndroidIntents&Bundles
Activity1:Sender Activity2:Receiver
IntentmyIntentA1A2=newIntent(Activity1.this,
Activity2.class);
BundlemyBundle1=newBundle();
myBundle1.putInt("val1",123);
myIntentA1A2.putExtras(myBundle1);
startActivityForResult(myIntentA1A2,1122);
y y
INTENT
requestCode (1122)
Senderclass/Receiverclass
66
requestCode (1122)
resultCode
Extras:{val1=123 }
10/18/2011
4
12.Android Intents Part2
Intents
AndroidIntents&Bundles
Activity1:Sender Activity2:Receiver y y
INTENT
C d (1122)
Senderclass/Receiverclass
IntentmyCallerIntent2=getIntent();
BundlemyBundle =myCallerIntent.getExtras();
int val1=myBundle.getInt("val1");
77
requestCode (1122)
resultCode
Extras:{val1=123 }
12.Android Intents Part2
Intents
AndroidIntents&Bundles
Activity1:Sender Activity2:Receiver y y
INTENT
C d (1122)
Senderclass/Receiverclass
myBundle.putString("val1",456);
myCallerIntent.putExtras(myBundle);
setResult(Activity.RESULT_OK,
myCallerIntent);
88
requestCode (1122)
resultCode (OK)
Extras:{val1=456}
10/18/2011
5
12.Android Intents Part2
Intents
AndroidBundlesAvailableat:http://developer.android.com/reference/android/os/Bundle.html
Example of Public Methods
void clear()
Removes all elements fromthe mapping of this Bundle.
Object clone() Object clone()
Clones the current Bundle.
boolean containsKey(String key)
Returns trueif the given key is contained in the mapping of this Bundle.
void putIntArray(String key, int[] value)
Inserts an int array value into the mapping of this Bundle, replacing any
existing valuefor thegiven key.
void putString(String key, String value)
Inserts a String valueinto themapping of this Bundle, replacing any existing
value for the given key.
void putStringArray(String key, String[] value)
Inserts a String array value into the mapping of this Bundle, replacing any
99
existing valuefor thegiven key.
void putStringArrayList(String key, ArrayList<String>value)
Inserts an ArrayList valueinto themapping of this Bundle, replacing any
existing valuefor thegiven key.
void remove(String key)
Removes any entry with thegiven key fromthemapping of this Bundle.
int size()
Returns thenumber of mappings contained in thisBundle.