How to Start Another Activity

This Android tutorial demonstrates how to start another activity from the application. In this example when user clicks the button in one activity, it launches another activity.

Create the Android Project. Select File>New>Android Project in the Eclipse menu.

Next create a class for first activity that extends the Activity class.

public class FirstActivity extends Activity

 

Create another class for second activity. This must also extend the Activity class.

public class SecondActivity extends Activity.

Create xml layout files.

Create activity_first.xml in the res/layout folder. This is used as resource layout file for FirstActivity.

<?xml version="1.0" encoding="utf-8"?>
<?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"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Click button to start a new activity"
    />
   
  <Button
   android:id="@+id/login_button"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="Click Me!"
   android:onClick="handleClick" />
</LinearLayout>

Create another resource layout file “activity_second.xml” for MainActivity class.

<?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"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello - This is second activity..."
    />
</LinearLayout>

Declare both the activities in AndroidManifest.xml file.

<activity android:name=".FirstActivity"
                  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=".SecondActivity"
                  android:label="@string/app_name">
        </activity>

       

In the onCreate() method of FirstActivity class set the laytout resources.

setContentView(R.layout.activity_first);

Create a method to handle the Button click event. Note activity_first.xml file declares the onClick property
for the Button as android:onClick="handleClick". In this case when a user clicks the button this method is called.

public void handleClick(View v){  }

When user clicks the button another activity is launched.  So the code to launch new activity goes inside the handleClick() method. Create an instance of the Intent in handleClick() method.

Intent intent = new Intent();

Then set the activity class that needs to be launched/started.

intent.setClass(this,SecondActivity.class);

finally, start the activity by calling

startActivity(intent);

Final code for FirstActivity.java

public class FirstActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        // Set the layout for this activity
        setContentView(R.layout.activity_first);
    }
   
    // Method to handle Button Click
    public void handleClick(View v){
      
     //Create an intent to start the new activity.
        // Our intention is to start secondActivity
     Intent intent = new Intent();
     intent.setClass(this,SecondActivity.class);
     startActivity(intent);
    }
}

Final code for SecondActivity.java

public class SecondActivity extends Activity {
   /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
     // Set the layout for this activity
        setContentView(R.layout.activity_second);
    }
}

Conclusion:

In this tutorial you learn how to launch another activity using Intent.

 

 

Comments

This is really a very helpful and well explained .......... thanks for providing such a beautiful explanation.

Haaahhha. I'm not too bright today. Great post!

this is really very very helpful to me.thank you for providing such a nice explanation.

very helpful to me . .

i've looked at so many examples and all were confusing EXCEPT for this one. been trying to figure out how to get the button to open a new activity for the fast few days. Thanks for the detailed explanation!

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.