MrBool
You must be logged in to give feedback. Click here to login
[Close]

You must be logged to download.

Click here to login

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

[Close]

MrBool is totally free and you can help us to help the Developers Community around the world

Yes, I'd like to help the MrBool and the Developers Community before download

No, I'd like to download without make the donation

Activities in Android

In this article well see how to create multiple activities and go from one activity to another and how to close all activities above the main one and return to the main activity.

[close]

You didn't like the quality of this content?

Would you like to comment what you didn't like?

So what exactly is an activity?

An activity is basically a screen for the user to interact with and do stuff on. See http://developer.android.com/guide/components/activities.html for a better explanation.

In this tutorial

For this tutorial I will show you how to create multiple activities and go from one activity to another and how to close all activities above the main one and return to the main activity. In this tutorial we will need to the AndroidManifest file among other things to achieve our goals. So here we go, we better get to it.

Andy Android activities

Figure 1: Andy Android activities

Creating and handling multiple activities.

Create a new Android Project in Eclipse and name it Application3. Name the main activity ApplicationActivity3.

Project Structure

Figure 2: Project Structure

Step 1:

As seen from the project structure above we create 2 more activities. Right click on your package name under src and select New then Class then enter Application3Activity2. Repeat the process for creating Application3Activity3. So now you have 3 java files. Now right click on layout in the res folder. Select New then select File then type activity2.xml. Repeat the process naming your activity activity3 for the final xml we need.

Step2:

Copy and paste this code into your string.xml file:

Listing 1: string.xml code

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="hello">Activity1</string>
    <string name="app_name">Application3</string>

</resources>

Step3:

Copy and paste this code into your main.xml file:

Listing 2: main.xml code

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" 
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="24sp"/>

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_marginTop="62dp"
        android:layout_gravity="center" />
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="20dp" 
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="24sp"
        android:text=""/>

    <Button
        android:id="@+id/goto_act2_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Go to Activity2" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="170dp" 
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="24sp"
        android:text=""/>

    <Button
        android:id="@+id/exit"
        android:layout_width="87dp"
        android:layout_height="wrap_content"
        android:text="Exit"
        android:layout_gravity="center" />

</LinearLayout>

You’ll end up with this:

main.xml Graphical Layout

Figure 3: main.xml Graphical Layout

Step 4:

Copy and paste this code into your activity2.xml file:

Listing 3: activity2.xml code

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="24sp"
        android:text="Activity2"/>

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_marginTop="62dp"
        android:layout_gravity="center" />
    
    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_gravity="center" />
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="20dp" 
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="24sp"
        android:text=""/>

    <Button
        android:id="@+id/goto_act3_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Go to Activity3" />

</LinearLayout>

You’ll end up with this:

activity2.xml Graphical Layout

Figure 4: activity2.xml Graphical Layout

Step 5:

Copy and paste this code into your activity3.xml file:

Listing 4: activity3.xml code

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

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="24sp"
        android:text="Activity3"/>

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_marginTop="62dp"
        android:layout_gravity="center" />
    
    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_gravity="center" />
    
    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        android:layout_gravity="center" />
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="20dp" 
        android:gravity="center"
        android:textColor="#000000"
        android:textSize="24sp"
        android:text=""/>

    <Button
        android:id="@+id/goto_act1_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Go to Activity1" />

</LinearLayout>

You’ll end up with this:

activity3.xml Graphical Layout

Figure 5: activity3.xml Graphical Layout

Step 6:

Copy and paste this code into Application3Activity (i,e, your main activity). Remember not to copy over your package name at the top of your file.

Listing 6: Application3Activity code

import android.R.color;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class Application3Activity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final Button goto_act2_btn = (Button) findViewById(R.id.goto_act2_btn);
        goto_act2_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
            	
            	startActivity(new Intent("net.learn2develop.APPLICATION3ACTIVITY2"));
            }
        });
        
        final Button exit = (Button) findViewById(R.id.exit);
        
        exit.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
            	
            	finish();
            }
        });
    }
}

You can see here how we start the second activity. By clicking the goto_act2_btn we use the code startActivity(new Intent("net.learn2develop.APPLICATION3ACTIVITY2")); to go the the second activity. This is a reference to what we will enter to the AndroidManiest file.

Step 7:

Now copy and paste this code into Application3Activity2.

Listing 7: Application3Activity2 code

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

public class Application3Activity2 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity2);
        
        final Button goto_act3_btn = (Button) findViewById(R.id.goto_act3_btn);
        goto_act3_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
            	
            	startActivity(new Intent("net.learn2develop.APPLICATION3ACTIVITY3"));
            }
        });
        
        
    }
}

Again the same way is used to go to the third activity of our application.

Step 8:

Now copy and paste this code into Application3Activity3

Listing 8: Application3Activity3

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

public class Application3Activity3 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity3);
        
        final Button goto_act1_btn = (Button) findViewById(R.id.goto_act1_btn);
        goto_act1_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
            	
            	 Intent intent=new Intent(Application3Activity3.this,Application3Activity.class);
            	 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
            	 startActivity(intent);
            }
        });
        
    }
}

Notice here we use:

Intent intent=new Intent(Application3Activity3.this,Application3Activity.class);
             intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
            	startActivity(intent);

instead of the previous ways shown. This way closes all activities above the main one and returns to the main activity.

Step 9:

Open the AndroidManifest.xml file in the res folder. Add this code to the code that is in between the application tags:

Listing 9: AndroidManifest.xml code

<activity android:name=".Application3Activity2"
					android:label="Application3">
			<intent-filter>
			<action android:name="net.learn2develop.APPLICATION3ACTIVITY2" />
			<category android:name="android.intent.category.DEFAULT" />
			</intent-filter>
		</activity>
		
        <activity android:name=".Application3Activity3"
					android:label="Application3">
			<intent-filter>
			<action android:name="net.learn2develop.APPLICATION3ACTIVITY3" />
			<category android:name="android.intent.category.DEFAULT" />
			</intent-filter>
		</activity>

So you will get something like this:

Listing 10: Manifest code

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="sc.applications.app3"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".Application3Activity"
            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=".Application3Activity2"
					android:label="Application3">
			<intent-filter>
			<action android:name="net.learn2develop.APPLICATION3ACTIVITY2" />
			<category android:name="android.intent.category.DEFAULT" />
			</intent-filter>
		</activity>
		
        <activity android:name=".Application3Activity3"
					android:label="Application3">
			<intent-filter>
			<action android:name="net.learn2develop.APPLICATION3ACTIVITY3" />
			<category android:name="android.intent.category.DEFAULT" />
			</intent-filter>
		</activity>
        
    </application>
    
    

</manifest>

Notice here the lines <activity android:name=".Application3Activity2" and especially <action android:name="net.learn2develop.APPLICATION3ACTIVITY3" as we reference this in the java files in order to open these activities.

Once you’ve done all that and you have no errors you’re ready to run the code and go from one activity to another. I placed one image button in the first activity, 2 in the second and 3 in the third. They are there to distinguish each activity like the TextViews do also on the top of the activities; the image buttons themselves do nothing when clicked.

Conclusion

I hope you learned something from this tutorial and enjoyed this tutorial. If so, just give it a like. I’ll see you all next time, Steve.

 Bye for now

Figure 6: Bye for now



Recently finished a Mobile technology course concentrating on Java and Android Development. Am currently working for a company developing Android applications.

What did you think of this post?
Services
Know how to keep MrBool Online
SUPPORT US
SUPPORT US
With your help, we can keep providing free content and helping you to be a better professional
support us
[Close]
To have full access to this post (or download the associated files) you must have MrBool Credits.

  See the prices for this post in Mr.Bool Credits System below:

Individually � in this case the price for this post is US$ 0,00 (Buy it now)
in this case you will buy only this video by paying the full price with no discount.

Package of 10 credits - in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download few videos. In this plan you will receive a discount of 50% in each video. Subscribe for this package!

Package of 50 credits � in this case the price for this post is US$ 0,00
This subscription is ideal if you want to download several videos. In this plan you will receive a discount of 83% in each video. Subscribe for this package!


> More info about MrBool Credits
[Close]
You must be logged to download.

Click here to login