Activity life cycle
Activity is the basic building block of every visible android application. It provides the means to render a GUI. Every screen in an application is an activity by itself. We can call each visible component as an activity in android. Though more than one activities work together to present an application sequence, each activity is an independent entity. Just have a look at the below image, which explains the life cycle of an activity. I will explain the practical use of an activity, when we will develop our first “Hello World” application in this tutorial.
Example program :
MainActivity
package com.techblogon.activitylifecycleexample;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(this, "On Create Called In First Activity", Toast.LENGTH_LONG).show();
Log.i("FirstActivity", "Inside onCreate");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Toast.makeText(this, "On Start Called In First Activity", Toast.LENGTH_LONG).show();
Log.i("FirstActivity", "Inside onStart");
}
@Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(this, "On Resume Called In First Activity", Toast.LENGTH_LONG).show();
Log.i("FirstActivity", "Inside onResume");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Toast.makeText(this, "On Pause Called In First Activity", Toast.LENGTH_LONG).show();
Log.i("FirstActivity", "Inside onPause");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Toast.makeText(this, "On Stop Called In First Activity", Toast.LENGTH_LONG).show();
Log.i("FirstActivity", "Inside onStop");
}
@Override
protected void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "On Destroy Called In First Activity", Toast.LENGTH_LONG).show();
Log.i("FirstActivity", "Inside onDestroy");
}
public void startSecondActivity(View V)
{
// create an new Intent and Start Second Activity
Intent intent=new Intent(this,SecondActivity.class);
startActivity(intent);
}
}
Xml coding for mainactivity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_gravity="center_horizontal"
android:textSize="23dp"
android:layout_marginTop="150dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This Is Fist Activity Activity"
/>
<Button
android:id="@+id/button1"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Second Activity "
android:onClick="startSecondActivity"/>
</LinearLayout>
Secound Activity:
package com.techblogon.activitylifecycleexample;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
public class SecondActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
Toast.makeText(this, "On Create Called In Second Activity", Toast.LENGTH_LONG).show();
Log.i("SecondActivity", "Inside onCreate");
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Toast.makeText(this, "On Start Called In Second Activity", Toast.LENGTH_LONG).show();
Log.i("SecondActivity", "Inside onStart");
}
@Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(this, "On Resume Called In Second Activity", Toast.LENGTH_LONG).show();
Log.i("SecondActivity", "Inside onResume");
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Toast.makeText(this, "On Pause Called In Second Activity", Toast.LENGTH_LONG).show();
Log.i("SecondActivity", "Inside onPause");
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Toast.makeText(this, "On Stop Called In Second Activity", Toast.LENGTH_LONG).show();
Log.i("SecondActivity", "Inside onStop");
}
@Override
protected void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(this, "On Destroy Called In Second Activity", Toast.LENGTH_LONG).show();
Log.i("SecondActivity", "Inside onDestroy");
}
}
secoundactivity xml coding:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_marginTop="150dp"
android:layout_gravity="center_horizontal"
android:textSize="23dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This Is Second Activity" />
</LinearLayout>
Output:
No comments:
Post a Comment