Class Business Activity o o An Activity is
Class Business
Activity o o An Activity is the main object in an Android app. Lifecycle:
Better Ways to Organize the Code o There are several times when the code is organized around an integer return value. n n n o on. Create. Dialog() on. Options. Item. Selected() on. Activity. Result() This results in a string of “if…then…else” code that just stinks.
Better Ways to Organize the Code o Use a table of classes. Do a table lookup of the code and Java reflection to call the right class.
Grade. Box Example
Starting New Activities o o A common action for an activity is to start another activity. When another activity is started, the current activity is stopped and the new activity is created/started/resumed.
Starting New Activities: Intents o o Intents are communications about classes or executables or data Example: communicate which activity to start by creating an intent that indicates the class to use
Starting New Activities o o Two ways to start an activity n To start an independent activity that will run, use start. Activity() n To start an activity that will return information to the current activity, use start. Activity. For. Result() Both calls use intents to indicate which activity should start up
start. Activity() Uri sms. Uri = Uri. parse("tel: "+grades. get. Student(item. Position). get. Mobile. Phone. Number()); Intent send. Intent = new Intent(Intent. ACTION_VIEW, sms. Uri); send. Intent. put. Extra("address", grades. get. Student(item. Position). get. Mobile. Phone. Number()); send. Intent. put. Extra("sms_body", "type message here"); send. Intent. set. Type("vnd. android-dir/mms-sms"); start. Activity(send. Intent);
start. Activity. For. Result() o o Using this call, one gives the intent AND a returning value that identifies the activity. When the Activity is completed, the method on. Activity. Result() is called.
start. Activity. For. Result() Intent game. Intent = new Intent(this, Game. Display. class); start. Activity. For. Result( game. Intent, R. id. class_display_games);
start. Activity. For. Result() protected void on. Activity. Result( int request. Code, int result. Code, Intent intent) { super. on. Activity. Result( request. Code, result. Code, intent); if (result. Code != RESULT_OK) return; if (intent != null) { if (request. Code == R. id. class_display_games) { … } } }
Grade. Box Example
Returning From an Activity o o o To halt an Activity, call finish() To return to the Activity that created the current Activity, call set. Result(): set. Result(RESULT_OK, result); This means automatic return with the ID that it was created with.
Grade. Box Example
Information in Intents o o You can insert information into an intent to communicate with the activity that is being started. The typical way is to 1. 2. 3. Create a Bundle Put things into the Bundle Add the Bundle to the Intent through put. Extras()
Grade. Box Example
Retrieving Information o o Use the get. Intent() call to retrieve the Intent Use the get…() call to get the extra you are looking for
Grade. Box Example
Exercise: XML File
Interface Component Overview
- Slides: 21