An Activity is nothing but a
screen in an app. For example the Gmap application the screen which displayed
the map is nothing but an activity. There will be situations where in you need
to traverse from one screen to another. In this post we get to know the
simplest way to traverse between activities.
Given below are the steps to
achieve this -
1) Define the second
activity in the android manifest Table.
2) Create 2 Layouts called
Default_Activity and Second_Activity with button in each of them, where in you
will traverse from Default_Activity screen to Second Activity.
3) Create 2 java classes for
each activity where you will define the action to be done on click of button in
each activity.
Now that we got the things that
need to be done lets look start coding the same
First Create a new Android
Project by clicking on the File and traversing to the New option in the menu.
In this case you can use normal Android 2.3 instead of Google Android 2.3 since
we don’t use any of the google API’s.
1) Defining Second Activity
in Android Manifest Table –
Open the Android Manifest
Table and add the following code which will define the second activity.
The android Manifest Table
will look like below
2) Creating the 2 layouts –
Go to res folder and click on the Layout
folder and a new xml file which will be your second activity.
Create the 2 activity using
the graphical layout as shown in the images below.
3) Defining Java classes –
a) Default_Activity class –
Instantiate the Button class
as shown below
Button Act1=(Button)
findViewById(R.id.button1);
Then write the code to move
to the second activity by using the OnClick function wherein you create an Intent which instructs the
application to go to the second activity.
public void onClick(View
view) {
Intent def_intent=new
Intent(view.getContext(),Second_Activity.class);
startActivityForResult(def_intent,0);
}
Finally your
Default_Activity class will look like this.
b) Second_Activity –
Create the onCreate function
and set the view to Second2activity as shown below
setContentView(R.layout.second2activity);
Create a new Button which
will end the app and return to the default activity screen and in the onClick
function use the code below
public void onClick(View v)
{
//creating a new intent
Intent sec_intent=new
Intent();
//set the result and return
to the default_activity Screen
setResult(RESULT_OK,sec_intent);
//End the Activity
finish();
The Second_Activity will
look like the screen shot shown below.
Now that we are done with
the coding, lets see if it really works as desired. Right Click on the project
and say Run as Android Application.
The initial screen will be
the one as shown below, click on the ‘Move to other Activity Button’
On clicking the button you
should come to screen 2 as shown below.
If you again click on the
button ‘Congrats you have come to the Second Activity’ it should take you back
to the Default Activity Screen.
Woohoooooo you have learnt
how to create an activity, in the next post I will let you know how to pass parameters
/ data between activities. Till then Adios!!!!!!
No comments:
Post a Comment