What is Activity?
Activities are
our application’s presentation layer. The UI of
your application is built around
one or more
extensions of the Activity
class. Activities use Fragments and Views to layout
and display
information, and to respond to user actions. Compared to desktop development,
Activities are equivalent to Forms.
Life Cycle methods of
Activity are used to control and manage the resource.
Following methods are available
android.app.Activity class.
- OnCreate: called when activity is first created.
- OnStart: called when activity is becoming visible to the user.
- OnResume: called when activity will start interacting with the user.
- OnPause: called when activity is not visible to the user.
- OnStop: called when activity is no longer visible to the user.
- OnRestart: called after your activity is stopped, prior to start.
- OnDestroy: called before the activity is destroyed.
Life Cycle Activity XML file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Activity_life_cycle" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="106dp"
android:layout_marginTop="110dp"
android:text="Finish" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="58dp"
android:ems="10" />
</RelativeLayout>
Life Cycle Activity .Java File:
package com.solomon.lifecycleactivity;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Activity_life_cycle extends Activity {
public static final String TAG="LifeCycleActivity";
Button finishButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_life_cycle);
finishButton = (Button)findViewById(R.id.button1);
finishButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
finish();
/*When we click this button our program will end and
onPause,OnStop,OnDestroy methods will be called.*/
}
});
Toast.makeText(getApplicationContext(),"Oncreate",Toast.LENGTH_LONG).show();
//Using Toast we can display some Pop-up messages.
/*In this program whenever oncreate or other methods are called
corresponding Toast message will appear on Emulator.*/
Log.d(TAG,"In Oncreate");
//This string will be displayed in LogCat.
}
@Override
protected void onDestroy() {
super.onDestroy();
Toast.makeText(getApplicationContext(),"OnDestroy",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnDestroy");
}
@Override
protected void onPause() {
super.onPause();
Toast.makeText(getApplicationContext(),"OnPause",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnPause");
}
@Override
protected void onRestart() {
super.onRestart();
Toast.makeText(getApplicationContext(),"OnRestart",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnRestart");
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Toast.makeText(getApplicationContext(),"OnRestoreInstanceState",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnRestoreInstanceState");
// called when the activity is started after it is destroyed by the system.
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(getApplicationContext(),"OnResume",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnResume");
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
/* When user change orientation of mobile device or Emulator
this method is called. Current state of Activity is saved when this
method is called. By Pressing Ctrl+F11 we can change the
orientation of Emulator.
*/
Toast.makeText(getApplicationContext(),"OnSaveInstanceState",
Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnSaveInstanceState");
}
@Override
protected void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(),"OnStart",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnStart");
}
@Override
protected void onStop() {
super.onStop();
Toast.makeText(getApplicationContext(),"OnStop",Toast.LENGTH_LONG).show();
Log.d(TAG,"In OnStop");
}
}
AndroidManifest.Xml File:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.solomon.lifecycleactivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.solomon.lifecycleactivity.Activity_life_cycle"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Output:
//Like this for every methods of life cycle Activity ,Toast message will appear.
LogCat Message:
/*If we change the orientation of mobile or Emulator
OnSaveInstanceState and OnRestoreInstanceState will be called.*/