|
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
RSS feed for comments to this post