|Articles & Tutorials|
|Android - How to switch between activities - Java|
|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;
And, very similar, our Activity2:
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:
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: