Android - How to switch between activities - Java PDF Print E-mail
Sunday, 26 June 2011 15:53

Activies are the main part of an Android application.

An application can be composed of several activies, and of course we need a way to switch between them.

Let's see how to do that.

For this tutorial, we'll create two activites, each one with a button that brings you to the other activity.

So, we create A button, put some self-explainatory text on it, and add a touch listener to detect touch inputs.

In the onTouch handler, we wait for a ACTION_UP event (practically, when the user release its finger from the screen).

When it occurs, we create a new intent, passing to it the current context and the Activity class we want to switch on. In this case, we want to go to Activity2, so we use Activity2.class .

Finally, we pass our new Intent to startActivity.

Here's our Activity1;


	package com.attiliocarotenuto.tutorials;

	 

	import android.app.Activity;

	import android.content.Intent;

	import android.os.Bundle;

	import android.view.MotionEvent;

	import android.view.View;

	import android.view.View.OnTouchListener;

	import android.widget.Button;

	 

	public class Activity1 extends Activity implements OnTouchListener{

	Button button;

	 

	@Override

	public void onCreate(Bundle savedInstanceState){

	      super.onCreate(savedInstanceState);

	      button = new Button(this);

	      button.setText("Activity 1, touch to pass to Activity 2");

	      button.setOnTouchListener(this);

	      setContentView(button);

	}

	 

	@Override

	public boolean onTouch(View view, MotionEvent event) {

	        switch(event.getAction()){

	        case MotionEvent.ACTION_UP:

	            Intent myIntent = new Intent(view.getContext(), Activity2.class);

	            startActivity(myIntent);

	            break;

	        }

	        return false;

	     }

	 

	}

	 

	
 
And, very similar, our Activity2:
 

	 

	
		package com.attiliocarotenuto.tutorials;
	
		 
	
		import android.app.Activity;
	
		import android.content.Intent;
	
		import android.os.Bundle;
	
		import android.view.MotionEvent;
	
		import android.view.View;
	
		import android.view.View.OnTouchListener;
	
		import android.widget.Button;
	
		 
	
		public class Activity2 extends Activity implements OnTouchListener{
	
		Button button;
	
		 
	
		@Override
	
		public void onCreate(Bundle savedInstanceState){
	
		      super.onCreate(savedInstanceState);
	
		      button = new Button(this);
	
		      button.setText("Activity 2, touch to pass to Activity 1");
	
		      button.setOnTouchListener(this);
	
		      setContentView(button);
	
		}
	
		 
	
		@Override
	
		public boolean onTouch(View view, MotionEvent event) {
	
		        switch(event.getAction()){
	
		        case MotionEvent.ACTION_UP:
	
		            Intent myIntent = new Intent(view.getContext(), Activity1.class);
	
		            startActivity(myIntent);
	
		            break;
	
		        }
	
		        return false;
	
		     }
	
		 
	
		}
	
		 
	
		
 

We're not done yet, we need to add our two activies to the manifest. Since this is a beginner tutorial, I'll also show you how to do it.   

Here's our manifest file:

	
		<?xml version="1.0" encoding="utf-8"?>
	
		<manifest xmlns:android="http://schemas.android.com/apk/res/android"
	
		      package="com.attiliocarotenuto.tutorials"
	
		      android:versionCode="1"
	
		      android:versionName="1.0">
	
		    <uses-sdk android:minSdkVersion="3" />
	
		 
	
		    <application android:icon="@drawable/icon" android:label="@string/app_name">
	
		 
	
		          <activity android:name="com.attiliocarotenuto.tutorials.Activity1"
	
		                        android:label="Activity 1">
	
		               <intent-filter>
	
		                       <action android:name="android.intent.action.MAIN" />
	
		                       <category android:name="android.intent.category.LAUNCHER" />
	
		               </intent-filter>
	
		          </activity>
	
		        
	
		          <activity android:name="com.attiliocarotenuto.tutorials.Activity2"
	
		                  android:label="Activity 2">
	
		          </activity>
	
		       
	
		    </application>
	
		</manifest>
	
		 
	
		
 
Since Activity1 is our application's entry-point, we specifies that intent filter on it.

Of course pay attention to the package name, since it is included in the manifest file.

We're done, here's the result:

 

 

Comments  

 
0 #4 Mrteach 2012-05-18 02:31
thanks :lol: for this tutorial in android activities.. very clear
Quote
 
 
+4 #3 RE: Android - How to switch between activities - JavaJon 2011-12-20 21:36
Aren't you just adding the same two activities to the stack that way? Are you sure that creating another Intent from Activity2 is the correct way to get back to Activity1?
Quote
 
 
0 #2 WebmasterAttilio 2011-12-11 20:26
Thank you very much, glad you found it useful!
Quote
 
 
0 #1 TechnoTalkative TechnoTalkative 2011-11-25 12:58
This is the great tutorial with perfect example, i was looking for the same.
Quote
 

Add comment


Security code
Refresh