Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects First with Java

Similar presentations


Presentation on theme: "Objects First with Java"— Presentation transcript:

1 Objects First with Java
Class Business © David J. Barnes and Michael Kölling

2 Activity An Activity is the main object in an Android app. Lifecycle:

3 Better Ways to Organize the Code
There are several times when the code is organized around an integer return value. onCreateDialog() onOptionsItemSelected() onActivityResult() This results in a string of “if…then…else” code that just stinks.

4 Better Ways to Organize the Code
Use a table of classes. Do a table lookup of the code and Java reflection to call the right class.

5 GradeBox Example

6 Starting New Activities
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.

7 Starting New Activities: Intents
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

8 Starting New Activities
Two ways to start an activity To start an independent activity that will run, use startActivity() To start an activity that will return information to the current activity, use startActivityForResult() Both calls use intents to indicate which activity should start up

9 startActivity() Uri smsUri = Uri.parse("tel:"+grades.getStudent(itemPosition). getMobilePhoneNumber()); Intent sendIntent = new Intent(Intent.ACTION_VIEW, smsUri); sendIntent.putExtra("address", grades.getStudent(itemPosition).getMobilePhoneNumber()); sendIntent.putExtra("sms_body", "type message here"); sendIntent.setType("vnd.android-dir/mms-sms"); startActivity(sendIntent);

10 startActivityForResult()
Using this call, one gives the intent AND a returning value that identifies the activity. When the Activity is completed, the method onActivityResult() is called.

11 startActivityForResult()
Intent gameIntent = new Intent(this, GameDisplay.class); startActivityForResult( gameIntent, R.id.class_display_games);

12 startActivityForResult()
protected void onActivityResult( int requestCode, int resultCode, Intent intent) { super.onActivityResult( requestCode, resultCode, intent); if (resultCode != RESULT_OK) return; if (intent != null) { if (requestCode == R.id.class_display_games) { … }

13 GradeBox Example

14 Returning From an Activity
To halt an Activity, call finish() To return to the Activity that created the current Activity, call setResult(): setResult(RESULT_OK, result); This means automatic return with the ID that it was created with.

15 GradeBox Example

16 Information in Intents
You can insert information into an intent to communicate with the activity that is being started. The typical way is to Create a Bundle Put things into the Bundle Add the Bundle to the Intent through putExtras()

17 GradeBox Example

18 Retrieving Information
Use the getIntent() call to retrieve the Intent Use the get…() call to get the extra you are looking for

19 GradeBox Example

20 Exercise: XML File

21 Interface Component Overview


Download ppt "Objects First with Java"

Similar presentations


Ads by Google