Sunday, July 3, 2011

Starting Another Android Activity With Intents

This tutorial will demonstrate how to start a second activity with Android and how to debug an Android ActivityNotFoundException.

Using Multiple Activities

Most Android projects involve using multiple activities. This tutorial will demonstrate how you can create start a second activity from your application, and pass along extras as well.

Create Two Layouts

First, we will create two different layouts, main.xml and secondactivity.xml

Screenshot of the main.xml layout


Screenshot of the secondactivity.xml layout

The source code for the activities is listed below.


main.xml
<?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" android:padding="5dip">
 <TextView android:layout_width="fill_parent"
  android:layout_height="wrap_content" android:text="Click the Button"></TextView>
 <Button android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:id="@+id/buttonClickButton"
  android:text="Click Me" android:layout_marginTop="10dip"></Button>
</LinearLayout>

secondactivity.xml
<?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" android:layout_margin="10dip">
 <TextView android:layout_width="wrap_content"
  android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"
  android:text="Replaced by extra value!" android:id="@+id/textViewExtraText"></TextView>
</LinearLayout>

Create MainActivity

The MainActivity will be the activity that is loaded when the application starts. The MainActivity will use the main.xml files as the layout, and when the button is pressed it will call the SecondActivity.

To start another activity in Android, we use something that is called an Intent. Intents basically tell the Android OS that you want to perform some type of action. Intents can name a specific class you would like to be called, or you can specify a general action, like Open up a Web Page, and the Android Operating System will help find the best application for the job.

The code that handles the button press is listed below. We create an intent that will start our SecondActivity. We then call startActivity and pass the newly created intent as a parameter.

MainActivity.java
package com.dreamdomm.intenttutorial;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button clickButton = (Button) findViewById(R.id.buttonClickButton);
        clickButton.setOnClickListener(new OnClickListener() {
   
   @Override
   public void onClick(View v) {
    Intent secondActivity = new Intent(getBaseContext(), SecondActivity.class);
    startActivity(secondActivity);
   }
  });
    }
}

Create SecondActivity

Now we need to create the code for the SecondActivity. In this tutorial, the code will be very simplistic. We will just load the secondactivity.xml layout and display it.

SecondActivity.java
package com.dreamdomm.intenttutorial;

import android.app.Activity;
import android.os.Bundle;

public class SecondActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.secondactivity);

 }

}

Run the App, and Error!

Now try running the app and pressing the click me button. You would expect the second activity to start, but instead you will be met with an error message.

Screenshot of the error in the android emulator

If you examine the logcat output, you can see some more details about the error. You can learn more about using logcat here.

Screenshot of the logcat output.
By examining the log, we can see that the error was an Android ActivityNotFoundException.

This means that Android can't find the activity that it is supposed to start. To remedy this problem, we need to update the manifest for the app so that there is a reference to the SecondActivity.

Update the Manifest

The manifest for the application contains a lot of different information that relates to your application as a whole, including all the activities your app uses. To add SecondActivity to this list, just add the following line before the </application> tag.

<activity android:name="SecondActivity"></activity>

Running the App

Now you should be able to run the app again without experiencing any errors.

Screenshot of the SecondActivity running in the emulator.

What's Next

Now that we have the basic foundation for calling different activities, we will build off of this in future tutorials. Areas that will be covered will include passing extras to activities (we will replace the text that says "replaced by extra value") and creating intents for specific actions.

1 comment:

  1. Very nice little tutorial, I'll be running through this shortly. Has the follow up been done? I'm looking to interface from a webview to my main activity and start+switch to a second activity passing various data to it. Then eventually return from the second activity to the main.

    ReplyDelete