This tutorial will show how to quickly create an Activity, and switch to another Activity using a ListView or a page full of buttons.
.Net folks can think of an Activity as a concept similar to a Form in a .Net Windows app. Each Activity you create is a new page in your app. An activity has essentially four states (blatantly stolen from the Android dev guide):
- If an activity in the foreground of the screen (at the top of the stack), it is active or running.
- If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
- If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
- If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.
To start, you should be familiar with the Hello, World and List View tutorials. Create a new Android project with 4 Class files:
- HomeListView.java
- Page1.java
- Page2.java
- Page3.java
Page1, Page2 and Page3 should each extend Activity, but HomeListView should extend ListActivity.
Under res/layout, create a new file called list_item.xml, and paste this xml into it:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
</TextView>
Create a string array to hold your ListView items by opening res/values/strings.xml and pasting the following inside the resources tag:
<string-array name="pages_array">
<item>Page 1</item>
<item>Page 2</item>
<item>Page 3</item>
<item>Switch to button mode</item>
</string-array>
Replace the onCreate method in HomeListView.java with the following code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] pages = getResources().getStringArray(R.array.pages_array);
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, pages));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myIntent = null;
if(((TextView) view).getText().equals("Page 1")){
myIntent = new Intent(view.getContext(), Page1.class);
}
if(((TextView) view).getText().equals("Page 2")){
myIntent = new Intent(view.getContext(), Page2.class);
}
if(((TextView) view).getText().equals("Page 3")){
myIntent = new Intent(view.getContext(), Page3.class);
}
//if(((TextView) view).getText().equals("Switch to button mode")){
//myIntent = new Intent(view.getContext(), ButtonPage.class);
//}
startActivity(myIntent);
}
});
}
Yes, the last if statement is commented out for now.
What we're doing here is creating an OnItemClickListener for the ListView and figuring out which item was clicked. The, we create an Intent to start a new Activity, and finally call startActivity to fire up the new activity (or new page, if you prefer).
You'll need to add all the activities to your main manifest file, so double click on AndroidManifest.xml, then click on the XML Source link to directly edit the xml. Paste the following xml in (you may already have HomeListView in there):
<activity android:name=".HomeListView" 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=".Page1"></activity>
<activity android:name=".Page2"></activity>
<activity android:name=".Page3"></activity>
The last step is to create something on each of the different Activity pages so you can see where you are. Paste the following code over the onCreate method in Page1.java, Page2.java and Page3.java (changing the "This is page 1" as appropriate...):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("This is page 1");
setContentView(tv);
}
Now, fire it up in the emulator. You should be able tselect page 1, 2 or 3 successfully.
What if you wanted to do the same thing using buttons instead of a ListView? To start, uncomment that fourth if statement in HomeListView.java, then create a new class called ButtonPage. In it's onCreate method, paste:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buttonpage);
Button page1 = (Button) findViewById(R.id.Button01);
page1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Page1.class);
startActivityForResult(myIntent, 0);
}
});
Button page2 = (Button) findViewById(R.id.Button02);
page2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Page2.class);
startActivityForResult(myIntent, 0);
}
});
Button page3 = (Button) findViewById(R.id.Button03);
page3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Page3.class);
startActivityForResult(myIntent, 0);
}
});
Button listViewPage = (Button) findViewById(R.id.Button04);
listViewPage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), HomeListView.class);
startActivityForResult(myIntent, 0);
}
});
Next, you'll need to create the file res/layout/buttonpage.xml and paste in:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="Page 1"
android:id="@+id/Button01"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px">
</Button>
<Button android:text="Page 2"
android:id="@+id/Button02"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px">
</Button>
<Button android:text="Page 3"
android:id="@+id/Button03"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px">
</Button>
<Button android:text="ListView Page"
android:id="@+id/Button04"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px">
</Button>
</LinearLayout>
Finally, open up AndroidManifest.xml once more and add:
<activity android:name=".ButtonPage"></activity>
Save everything and run. Now, when you click on the "Switch to button mode" item in the ListView, you'll jump to a screen with 4 buttons that should work similarly to your ListView.
Updated (2/23/2012): I've added the complete Eclipse project file for this tutorial to DropBox: ActivitySwitchingBlogDemo1.zip
Technorati Tags: Android