210 likes | 225 Vues
Class Business. Activity. An Activity is the main object in an Android app. Lifecycle:. Better Ways to Organize the Code. There are several times when the code is organized around an integer return value. onCreateDialog() onOptionsItemSelected() onActivityResult()
E N D
Activity • An Activity is the main object in an Android app. • Lifecycle:
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.
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.
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.
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
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
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);
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.
startActivityForResult() Intent gameIntent = new Intent(this, GameDisplay.class); startActivityForResult( gameIntent, R.id.class_display_games);
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) { … } } }
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.
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()
Retrieving Information • Use the getIntent() call to retrieve the Intent • Use the get…() call to get the extra you are looking for