mobappdev (fall 2014): getting results from multiple activities
DESCRIPTION
TRANSCRIPT
MobAppDev
Getting Results from
Multiple Called Activities
Vladimir Kulyukin
www.vkedco.blogspot.com
Outline
● Review● Coding Lab: Computing Fibonacci & Prime Numbers with
Activities Called by Explicit or Implicit Intents
Review
Caller & Called Activities
● A caller activity is an activity that launches another activity
● A launch may happen with an explicit or an implicit intent
● A called activity is an activity launched by another activity
What Does the Caller Do?● The caller activity must define request codes
● A request code is an integer constant (with a meaningful name!) that specifies a specific type of request (e.g., compute some math function, grayscale an image, retrieve some information from a data source)
● The caller & called activities must share the data keys they use to pass data back and forth via intents
● The caller must call startActivityForResult() to launch a called activity
● The caller must implement onActivityResult() to get the result back from a called activity
startActivityForResult()
● startActivityForResult(Intent intntForCalledAct, int request_code)
● Intent intntForCalledAct is created by the caller
● Intent intntForCalledAct specifies a called activity explicitly or implicitly through some action and passes data to the called activity via a Bundle
● request_code is a constant shared by the caller and the called
● request_code is used by the called activity to figure out what the caller wants it to do
● request_code is used by the caller to get the results back from the child
onActivityForResult()
● onActivityForResult(int request_code, int result_code, Intent data)
● This callback method must be implemented by the caller: it is called when a called activity returns the results to the caller
● request_code is a constant shared by the caller and the called activities
● result_code is a constant specifying whether the computation performed by the called activity was successful or not (e.g., Activity.RESULT_OK or Activity.RESULT_CANCELED)
What Does the Called Activity Do?● The called activity receives an Intent from its caller
● The called activity uses the request code to figure out what needs to be done
● The called activity uses the caller's intent to get specifics on what needs to be done (e.g., input parameters)
● The called activity must create an Intent returnData
● The called activity must perform the required computation
● The called activity must Activity.setResult(result_code, Intent returnData) to return the results back to its caller
Computing Fibonacci & Prime Numbers with Activities Started by
Explicit or Implicit Intents
source code is hereif the link does not work, the full url is
https://github.com/VKEDCO/CallerAndCalledActivities/blob/master/CallerAndCalledActivities.zip
Application Specs● Develop an application that allows the user compute n-th
fibonacci or prime number through activities called by explicit or implicit intents
● The application has three activities: one caller activity (CallerAct) & two called activities (CalledFibonacciNumbersAct & CalledPrimeNumbersAct)
● CallerAct uses startActivityForResult() to call the called activities and onActivityResult() to display returned results
Solution Steps● Design UI● Define Request Codes● Define Data Keys● Define Actions for Implicit Intent Filters● Implement Caller & Called Activities● Test & Debug
Design UI
UI Layout
UI Functionality● Click on “Expl Intent Fibo” uses startActivityForResult() to
call CalledFibonacciNumbersAct by explicit intent to compute 10th fibonacci number
● Click on “Impl Intent Fibo” uses startActivityForResult() to call CalledFibonacciNumbersAct by implicit intent to compute 10th fibonacci number
● Click on “Expl Intent Prime” uses startActivityForResult() to call CalledPrimeNumbersAct by explicit intent to compute 5th prime
● Click on “Impl Intent Prime” uses startActivityForResult() to call CalledPrimeNumbersAct by implicit intent to compute 5th prime
● onActivityResult() is used to toast returned results
Define Request Codes
Request Codes public class StartActivityForResultCallerAct extends Activity
implements OnClickListener {
// request codes
final static int FIBO_REQUEST_CODE = 1;
final static int PRIME_REQUEST_CODE = 2;
…
}
● FIBO_REQUEST_CODE is used in startActivityForResult() to call CalledFibonacciNumbersAct
● PRIME_REQUEST_CODE is used in startActivityForResult() to call CalledPrimeNumbersAct
Define Data Keys
Data Keys for Intent Bundles <string name="fibo_request_key">fibo_request</string>
<string name="fibo_result_key">fibo_result</string>
<string name="prime_request_key">prime_request</string>
<string name="prime_result_key">prime_result</string>
● fibo_request_key is used by the caller CallerAct to pass the parameter to the callee CalledFibonacciNumbersAct
● fibo_result_key is used by the callee CalledFibonacciNumbersAct is return n-th fibonacci number to the caller
● prime_request_key is used by the caller CallerAct to pass the parameter to the callee CalledPrimeNumbersAct
● prime_result_key is used by the callee CalledPrimeNumbersAct to return the return the n-th prime to the caller
Define Actions for Implicit Intent Resolution
Intent Filter Actions for Implicit Intent Resolution
public class StartActivityForResultCallerAct extends Activity
implements OnClickListener {
// implicit intents to launch called activities
final static String COMPUTE_FIBO_ACTION = "org.vkedco.android.mobicom.fibonacci";
final static String COMPUTE_PRIME_ACTION = "org.vkedco.android.mobicom.primes";
…
}
● Action strings are defined in the caller (use meaningful names!)
● These strings must be the same as the ones used in AndroidManifest.xml in the intent filters of the the called activities
Intent Filter Actions in AndroidManifest.xml <activity android:name="org.vkedco.mobicom.android.start_sum_activity_for_result_app.CalledFibonacciNumbersAct" android:label="@string/app_name" > <intent-filter>
<action android:name="org.vkedco.android.mobicom.fibonacci" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> <activity android:name="org.vkedco.mobicom.android.start_sum_activity_for_result_app.CalledPrimeNumbersAct" android:label="@string/app_name" > <intent-filter>
<action android:name="org.vkedco.android.mobicom.primes" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity>
Implement Caller & Called Activities
Calling Activities by Explicit Intent public void onClick(View v) { Button clickedButton = (Button) v; int nth; switch ( clickedButton.getId() ) { case R.id.btnExplFibcn: try { nth = Integer.parseInt(mEdTxtFiboN.getText().toString()); } catch (NumberFormatException nfe) { Toast.makeText(getApplicationContext(), nfe.toString(), Toast.LENGTH_LONG).show(); return; } Intent explFiboIntent = new Intent(this, CalledFibonacciNumbersAct.class); exlFiboIntent.putExtra(mRes.getString(R.string.fibo_request_key), nth); startActivityForResult(explFiboIntent, FIBO_REQUEST_CODE); return; // rest of code}
Calling Activities by Implicit Intent public void onClick(View v) { Button clickedButton = (Button) v; int nth; switch ( clickedButton.getId() ) { case R.id.btnImplFibcn: try { nth = Integer.parseInt(mEdTxtFiboN.getText().toString()); } catch (NumberFormatException nfe) { Toast.makeText(getApplicationContext(), nfe.toString(), Toast.LENGTH_LONG).show(); return; } Intent implFiboIntent = new Intent(COMPUTE_FIBO_ACTION); implFiboIntent.putExtra(mRes.getString(R.string.fibo_request_key), nth); startActivityForResult(implFiboIntent, FIBO_REQUEST_CODE); return; // rest of code}
How Called Activities Return Results to Callers// this is onCreate() in CalledFibonacciSumsAct protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Intent requestIntent = this.getIntent(); // get the caller's intent Resources res = this.getResources(); // check if the caller's intent has the required data key if ( requestIntent.hasExtra(res.getString(R.string.fibo_request_key))) { // fibo is the method that computes n-th fibonacci number int rslt = fibo(requestIntent.getIntExtra(res.getString(R.string.fibo_request_key), -1));; if ( rslt == -1 ) { this.setResult(RESULT_CANCELED); this.finish(); // finish and return RESULT_CANCELLED to the caller } else { Intent resultIntent = new Intent(); resultIntent.putExtra(res.getString(R.string.fibo_result_key), rslt); // put the result into the intent under the appropriate data key this.setResult(RESULT_OK, resultIntent); // return intent to the caller with RESULT_OK this.finish(); } } else { this.setResult(RESULT_CANCELED); this.finish(); }}
How Callers Process Returned Results// this is onActivityResult in CallerAct public void onActivityResult(int requestCode, int resultCode, Intent returnedData) {
super.onActivityResult(requestCode, resultCode, returnedData); switch ( requestCode ) { case FIBO_REQUEST_CODE: if ( resultCode == Activity.RESULT_OK ) { if ( returnedData.hasExtra(mRes.getString(R.string.fibo_result_key)) ) { Toast.makeText(this, "Fibo = " + returnedData.getIntExtra(mRes.getString(R.string.fibo_result_key), -1), Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "Fibo Computation Failure", Toast.LENGTH_LONG).show(); } } return; // cases for other request codes}