Slide 3©SoftMoore Consulting (the main activity) (a second activity) Also true for services and content providers. Broadcast receivers can be declared in the manifest or created/registered dynamically."> Slide 3©SoftMoore Consulting (the main activity) (a second activity) Also true for services and content providers. Broadcast receivers can be declared in the manifest or created/registered dynamically.">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started.

Similar presentations


Presentation on theme: "Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started."— Presentation transcript:

1 Applications with Multiple Activities

2 Most applications will have more than one activity. The main activity is started when the application is started. The main activity can launch another activity, usually in response to some event such as a button click. The launching of another activity causes the main activity to pause while the second activity is active. When the second activity finishes, the main activity is brought to the foreground and resumed. Slide 2©SoftMoore Consulting

3 Declaring Activities in the Android Manifest Every activity must be declared in AndroidManifest.xml. <activity android:name=".MainActivity" android:label="@string/app_name" > <activity android:name=".Activity2" android:label="@string/activity2" > Slide 3©SoftMoore Consulting (the main activity) (a second activity) Also true for services and content providers. Broadcast receivers can be declared in the manifest or created/registered dynamically.

4 Forcing Single Task Mode It is possible to navigate away from an activity and then relaunch it again, leading to multiple instances of the activity on the device. Eventually the redundant instances of the activity are killed to free up memory, but in the meantime, their existence can be confusing. To insure that only one instance of an activity runs on the device, for any activity that has MAIN and LAUNCHER intent filters, modify the activity element in AndroidManifest.xml to add the following attribute: android:launchMode="singleInstance" Slide 4©SoftMoore Consulting

5 Using Intents Use an intent to launch another application component such as an activity or a service. Recall that an explicit intent specifies the component to start by name. An explicit intent is typically used to start a component within the same application. Slide 5©SoftMoore Consulting

6 Example: Using an Explicit Intent to Launch a Second Activity // in the onCreate() method for MainActivity Button activity1Button = (Button)findViewById(R.id.activity1Button); activity1Button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, Activity2.class); startActivity(intent); } }); Slide 6©SoftMoore Consulting

7 Designing Applications with Multiple Activities If the main activity launches a second activity using an intent, then the second activity should not normally re-launch the main activity using an intent. This would cause multiple instances of the main activity to exist on the activity stack. Instead, the second activity would normally call its finish() method to return to the main activity, usually in response to some event such as a button click within the second activity. Slide 7©SoftMoore Consulting

8 Example: Returning to the Main Activity // in the onCreate() method for Activity2 Button activity2Button = (Button)findViewById(R.id.activity2Button); activity2Button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); Slide 8©SoftMoore Consulting

9 Using Intents for Inter-Activity Communication Extras are key-value pairs that provide additional information to the component handling the intent. When one activity launches a second activity, the first activity can attach “extra” data to the Intent used to invoke the second activity in a manner somewhat analogous to passing parameters in a method call. When the second activity finishes, it can attach “extra” data to an Intent and return it back to the first activity in a manner somewhat analogous to methods returning values. Slide 9©SoftMoore Consulting

10 Attaching Extra Data to an Intent Selected “put” methods Intent putExtra(String name, boolean value) Intent putExtra(String name, double value) Intent putExtra(String name, int value) Intent putExtra(String name, Serializable value) Intent putExtra(String name, String value) Selected “get” methods boolean getBooleanExtra(String name, boolean defaultValue) double getDoubleExtra(String name, double defaultValue) int getIntExtra(String name, int defaultValue) Serializable getSerializableExtra(String name) String getStringExtra(String name) Slide 10©SoftMoore Consulting

11 Attaching Extra Data to an Intent (continued) There are array versions of each of the above methods; e.g. Intent putExtra(String name, boolean[] value) Intent putExtra(String name, String[] value) Slide 11©SoftMoore Consulting

12 Launching a New Activity The method startActivity() can be used to launch a new activity when no result is to be returned. The method startActivityForResult() can be used to launch a new activity for which you would like a result when it has finished. When using startActivityForResult(), you should also implement method onActivityResult() to retrieve the “extras” returned from the launched activity. Slide 12©SoftMoore Consulting

13 Example: Launching an Activity for a Result Slide 13©SoftMoore Consulting

14 Example: Launching an Activity for a Result (in class MainActivity ) private static final int EDIT_ACTIVITY = 1; private String name = "John Moore";... // in the onCreate() method Button mainActivityButton = (Button) findViewById(R.id.mainActivityButton); mainActivityButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, Activity2.class); intent.putExtra("name", name); startActivityForResult(intent, EDIT_ACTIVITY); } }); Slide 14©SoftMoore Consulting second integer parameter identifies the call

15 Retrieving Extras from an Intent // in the onCreate() method of Activity2 Intent intent = getIntent(); String name = intent.getStringExtra("name"); EditText editText = (EditText) findViewById(R.id.editText); editText.setText(name); Slide 15©SoftMoore Consulting

16 Screen for Activity2 Slide 16©SoftMoore Consulting

17 Another Screen for Activity2 Slide 17©SoftMoore Consulting

18 Returning an Intent as a Result (in class Activity2 ) Button submitButton = (Button) findViewById(R.id.submitButton); submitButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText editText = (EditText) findViewById(R.id.editText); Intent intent = new Intent(); intent.putExtra("name", editText.getText().toString()); setResult(RESULT_OK, intent); finish(); } }); Slide 18©SoftMoore Consulting

19 Example: Retrieving Extras from a Result (in class MainActivity ) @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == EDIT_ACTIVITY && resultCode == RESULT_OK) { name = intent.getStringExtra("name"); TextView textView = (TextView) findViewById(R.id.mainActivityTextView); textView.setText(name); } Slide 19©SoftMoore Consulting second integer parameter from the original call

20 Example: Retrieving Extras from a Result (continued) Slide 20©SoftMoore Consulting

21 Relevant Links Opening a New Screen http://developer.android.com/guide/faq/commontasks.html#opennewscreen Starting an Activity http://developer.android.com/guide/components/activities.html#StartingAnActivity Slide 21©SoftMoore Consulting


Download ppt "Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started."

Similar presentations


Ads by Google