Slide 1

Applications with Multiple Activities
Applications with Multiple Activities
• Most applications will have more than one activity.
• Every activity must be declared in AndroidManifest.xml.
– Also true for services and content providers.
– Broadcast receivers can be declared in the manifest or
created/registered dynamically.
• 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.
©SoftMoore Consulting
Slide 2
Declaring Activities in the Android Manifest
<activity
android:name=".MainActivity"
main
android:label="@string/app_name"
activity
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="@string/title_activity_second"
android:theme="@style/AppTheme.NoActionBar">
</activity>
©SoftMoore Consulting
second
activity
Slide 3
Activity Launch Modes
•
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.
•
Attribute android:launchMode can be added to an
activity in AndroidManifest.xml to provide additional
control over how many instances can be launched.
©SoftMoore Consulting
Slide 4
Values for android:launchMode
•
Normal launches for most activities
– standard (default): The system always creates a new instance
of the activity in the target task and routes the intent to it.
– singleTop: If an instance of the activity already exists at the top
of the target task, the system routes the intent to that instance
rather than creating a new instance of the activity.
•
Specialized launches (not recommended for general use)
– singleTask: If an instance of the activity already exists, the
system routes the intent to the existing instance rather than
creating a new one.
– singleInstance: Similar to singleTask except that the activity
is always the single and only member of its task. The system will
not launch any other activity into the task holding this type.
©SoftMoore Consulting
Slide 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.
©SoftMoore Consulting
Slide 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, SecondActivity.class);
startActivity(intent);
}
});
©SoftMoore Consulting
Slide 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.
©SoftMoore Consulting
Slide 8
Example: Returning to the Main Activity
// in the onCreate() method for SecondActivity
Button activity2Button =
(Button) findViewById(R.id.activity2Button);
activity2Button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
finish();
}
});
©SoftMoore Consulting
Slide 9
Example: Two Activities
©SoftMoore Consulting
Slide 10
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.
©SoftMoore Consulting
Slide 11
Attaching Extra Data to an Intent
•
Selected “put” methods
Intent
Intent
Intent
Intent
Intent
•
putExtra(String
putExtra(String
putExtra(String
putExtra(String
putExtra(String
name,
name,
name,
name,
name,
boolean value)
double value)
int value)
Serializable value)
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)
©SoftMoore Consulting
Slide 12
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)
©SoftMoore Consulting
Slide 13
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.
©SoftMoore Consulting
Slide 14
Example: Launching an Activity for a Result
©SoftMoore Consulting
Slide 15
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, SecondActivity.class);
intent.putExtra("name", name);
startActivityForResult(intent, EDIT_ACTIVITY);
}
second integer parameter identifies the call
});
©SoftMoore Consulting
Slide 16
Retrieving Extras from an Intent
// in the onCreate() method of SecondActivity
Intent intent = getIntent();
String name = intent.getStringExtra("name");
EditText editText = (EditText) findViewById(R.id.editText);
editText.setText(name);
©SoftMoore Consulting
Slide 17
Initial Screen for Second Activity
©SoftMoore Consulting
Slide 18
Another Screen for Second Activity
©SoftMoore Consulting
Slide 19
Returning an Intent as a Result
(in class SecondActivity)
private void returnToMainActivity()
{
EditText editText = (EditText) findViewById(R.id.editText);
Intent intent = new Intent();
intent.putExtra("name", editText.getText().toString());
setResult(RESULT_OK, intent);
finish();
}
Use the button’s OnClickListener and/or the edit
text’s OnEditorActionListener to call this method.
©SoftMoore Consulting
Slide 20
Example: Retrieving Extras from a Result
(in class MainActivity)
@Override
protected void onActivityResult(int requestCode,
int resultCode, Intent intent)
{
second integer parameter
if (requestCode == EDIT_ACTIVITY
from the original call
&& resultCode == RESULT_OK)
{
name = intent.getStringExtra("name");
TextView textView = (TextView)
findViewById(R.id.mainActivityTextView);
textView.setText(name);
}
}
©SoftMoore Consulting
Slide 21
Example: Retrieving Extras from a Result
(continued)
©SoftMoore Consulting
Slide 22
Relevant Links
•
Starting Another Activity
https://developer.android.com/training/basics/firstapp/starting-activity.html
•
Getting a Result from an Activity
https://developer.android.com/training/basics/intents/result.html
©SoftMoore Consulting
Slide 23