Java – Creating and Switching to a new Activity in Android

Below is a small tutorial showing you how to create a new Android Activity and Switch to it whenever needed as well as passing information between them, basically, this will allow you to switch to a new interface such as an options page for example, when required.

Creating the Layout

You’ll need to add an XML file to the layout folder in Eclipse, you can do this by right clicking the folder and choosing New. Call it something resembling the new Activity, e.g. About. Once it’s created switch to manual editing mode (at the bottom) and add the following (if you don’t add any XML by default Eclipse will throw an error) for this example I’ll be calling it about.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_height="wrap_content" android:gravity="center_horizontal"
	android:layout_width="fill_parent" android:orientation="vertical">
</LinearLayout>

Once this has been added you can switch back to the GUI editor and change it as you wish, but it needs some base XML in or it won’t load.

Creating the Activity Class

You can create a new class by right clicking in your package and choosing New > Class and add the following code. Notice that this resembles the default Activity when your project was created, except it’s displaying a new Layout. Notice that I called by new class AboutActivity and called by new layout R.layout.about


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

public class AboutActivity  extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.about);
	}

}

Now that both the new Activity Class and Layout are both in place, you need some way of switching between these applications. One easy way to do this is by using Android Intents, using Intents you can also pass information between two Activities when needed.

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

Switching to our new Activity

Make sure to define i as an Intent in the class you’re using this code in. You will also need to Import android.content.Intent and android.app.PendingIntent, just let Eclipse add these for you.

i = new Intent(this, AboutActivity.class);
startActivity(i);

Once executed the above will switch to the new Activity and load the respective Layout for it.

Passing information between Activities

This one will be a very short example, taking the above and adding an extra line using putExtra.

i = new Intent(this, AboutActivity.class);
i.putExtra("aboutText", "Some useful text here!");
startActivity(i);

You’ll now be able to do the following in your new Activity onCreate method:

Intent i = getIntent();
String about = i.getStringExtra("aboutText");

Now you should be able to Create a new activity, create a new layout for your Activity and pass basic information between Activities.

Enjoy.

About Steve

Web Developer, IT enthusiast & PC Gamer.
This entry was posted in Android and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>