21
Applications with Multiple Activities

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

Embed Size (px)

Citation preview

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

Applications with Multiple Activities

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

©SoftMoore Consulting

Applications with Multiple Activities

• 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

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

©SoftMoore Consulting

Declaring Activities in the Android Manifest

Every activity must be declared in AndroidManifest.xml. <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

<activity android:name=".Activity2" android:label="@string/activity2" > </activity>

Slide 3

(the main activity)

(a second activity)

Also true for services and content providers. Broadcast receiverscan be declared in the manifest or created/registered dynamically.

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

©SoftMoore Consulting

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

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

©SoftMoore Consulting

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

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

©SoftMoore Consulting

Example: Using an Explicit Intentto 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

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

©SoftMoore Consulting

Designing Applications with Multiple Activities

• If the main activity launches a second activity using an intent, then the second activity should not normallyre-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

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

©SoftMoore Consulting

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

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

©SoftMoore Consulting

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

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

©SoftMoore Consulting

Attaching Extra Data to an Intent

• Selected “put” methodsIntent 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” methodsboolean 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

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

©SoftMoore Consulting

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

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

©SoftMoore Consulting

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

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

©SoftMoore Consulting

Example: Launching an Activity for a Result

Slide 13

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

©SoftMoore Consulting

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() methodButton 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

second integer parameter identifies the call

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

©SoftMoore Consulting

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

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

©SoftMoore Consulting

Screen for Activity2

Slide 16

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

©SoftMoore Consulting

Another Screen for Activity2

Slide 17

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

©SoftMoore Consulting

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

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

©SoftMoore Consulting

Example: Retrieving Extras from a Result(in class MainActivity)

@Overrideprotected 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

second integer parameterfrom the original call

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

©SoftMoore Consulting

Example: Retrieving Extras from a Result(continued)

Slide 20

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

©SoftMoore Consulting

Relevant Links

• Opening a New Screenhttp://developer.android.com/guide/faq/commontasks.html#opennewscreen

• Starting an Activityhttp://developer.android.com/guide/components/activities.html#StartingAnActivity

Slide 21