Android activity lifecycle

When a new project in android is created. You have a fully working android application.

In the source folder we have Main.java class file. Below is the code for the newly created java file

Main.java

package com.myfirstapp.activities;

import android.app.activity;
Public class Main extends Activity
{

@Override

// THE Below function is called when the activity is first created

public void onCreate(Bundle savedInstanceState)
{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);
}

}

 

The Main.java defines a new activity. It consists of two parts

Main.java file that contains the source code

main.xml file : This file is stored in res/layout folder. This file contains the user interface.

The setContentView(R.layout.main) method is used to call the main.xml (user interface file) from the res/layout folder.

 

Once this method is called , it loads the main.xml file and it shows the design that you have created.

The layout file main.xml file can be controlled by two ways

1) Using the interface provided by the android

2) You can also edit the xml file

The top tag in the main.xml file in the android activity life cycle is to define the layout.

There are different layouts in the android activity life cycle. The default layout that is used is linearlayout

<?xml version=”1.0″ encoding=””utf-8″?>

<LinearLayout>

<TextView android:id=”@+id/textView1″ android:layout_widt=”wrap_content” />

</LinearLayout>

2) How to connect two Activities :
In order to connect two activities, we will crate a new activity. Creating a new activity consists of two parts:

a) Creating the class file in the src folder

b) creating the layout file , to display the userinterface in the layout folder.
Right click on the packagename in the src folder and create a new class.

In the super class choose the “activity” class to which this new class will extend.

once the class is created. The second step is to create the layoutfile.
Right click on the “res/layout” folder and create a new layout file.

Now in order to connect two activities in android activity life cycle. Create a button in the first activity layout file.

By default android assigns an id to the new button.

Once the button is created in the xml file of the first layout. Write the following code in order to invoke the new
activity class.
package com.myfirstapp.activities;

import android.app.Activity;
public class Main extends Activity

{

@Override

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

// First get the reference of the button control that is created in the xml file

Button b=(Button)findViewById(R.id.button1);

// now b contains the reference of the button. button1 is the id of the button created
in the xml file

b.setOnClickListener(new OnClickListener() {

public void onClick(View v)
{
startActivity(new Intent(Main.this,Second.class);

}

});
}

In order to invoke the new class named (second.java). We use the Intent class. The intent class is basically used to
broadcast in the system, that, which activity is going to call the other activity.

In the above example you can see we are passing two parameters.

1)Main.this : this is the class which is sending message to the other activity named (Second.class)

So this way two activity interacts in android activity life cycle.

 

android-developer-tutorial