Open Activities Using Intents
Description
An Android application can contain zero or more activities.
In Android, you navigate between activities through what is known as an intent.
Example
Set up the manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.java2s.app" >
/*from w w w . j a v a2 s . co m*/
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.java2s.app.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:label="Second Activity"
android:name=".SecondActivity" >
<intent-filter >
<action android:name="com.java2s.SecondActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Second Activity
package com.java2s.app;
/*from w w w. j a v a 2 s. c o m*/
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout parentContainer = new LinearLayout(this);
parentContainer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
parentContainer.setOrientation(LinearLayout.VERTICAL);
Button button = new Button(this);
parentContainer.addView(button);
button.setText("Second");
setContentView(parentContainer);
}
}
Main activity to load the second activity:
package com.java2s.app;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
//w w w .ja v a2 s. c o m
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout parentContainer = new LinearLayout(this);
parentContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
parentContainer.setOrientation(LinearLayout.VERTICAL);
Button button = new Button(this);
button.setText("Open");
parentContainer.addView(button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent("com.java2s.SecondActivity"));
}
});
setContentView(parentContainer);
}
}
Note
The intent filter name for the new activity is com.java2s.SecondActivity
.
Other activities that wish to call this activity will invoke it via this name.
The category for the intent filter is android.intent.category.DEFAULT
.
So that this activity can be
started by another activity using the startActivity()
method.
Activities in Android can be invoked by any application running on the device.
To invoke is defined within the same project, you can rewrite the preceding example like this:
startActivity(new Intent(this, SecondActivity.class));