An activity is a single, focused thing that the user can do.
Almost all activities interact with the user, so the Activity class takes care
of creating a window for you in which you can place your UI with setContentView(View).
Background
Activity is a window that contains the user interface of
your application. As there are various states of activity like Running, Paused,
Stopped and Killed.
Activity base class contains events that govern the life
cycle of an activity.
- onCreate(): Called when the activity is first created
- onStart(): Called when the activity becomes visible to the user
- onResume(): Called when the activity starts interacting with the user
- onPause(): Called when the current activity is being paused and the previous activity is being resumed
- onStop(): Called when the activity is no longer visible to the user
- onDestroy(): Called before the activity is destroyed by the system
- onRestart(): Called when the activity has been stopped and is restarting again
Using the Code
To create an activity, your class need to extends Activity
base class. An Application has more than one activity.
packagesat.example.lifecycle;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.util.Log;
importandroid.widget.Toast;
public class LifeCycleActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Toast.makeText(LifeCycleActivity.this,"ON CREATE",
Toast.LENGTH_SHORT).show();
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Toast.makeText(LifeCycleActivity.this,"ON START",
Toast.LENGTH_SHORT).show();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Toast.makeText(LifeCycleActivity.this,"ON RESUME",
Toast.LENGTH_SHORT).show();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Toast.makeText(LifeCycleActivity.this,"ON PAUSE",
Toast.LENGTH_SHORT).show();
}
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Toast.makeText(LifeCycleActivity.this,"ON
RESTART", Toast.LENGTH_SHORT).show();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Toast.makeText(LifeCycleActivity.this,"ON STOP",
Toast.LENGTH_SHORT).show();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Toast.makeText(LifeCycleActivity.this,"ON
DESTROY", Toast.LENGTH_SHORT).show();
}
}
- When you run the following code, you see when the application starts there are three toast messages that come after one another. First is ON CREATE , second is ON START and third is ON RESUME.
- When you press the home button from the emulator, ON PAUSE and ON STOP methods call.
- And when you open the application again ON RESTART, ON START and ON RESUME methods call.
- And at last, when you press the back button from emulator, ON PAUSE , ON STOP and ON DESTROY method calls.
It provides the details about the invocation of life cycle
methods of activity. In this example, we are displaying the content on the
logcat.
File: MainActivity.java
package com.example.activitylifecycle;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("lifecycle","onCreate invoked");
}
@Override
protected void onStart() {
super.onStart();
Log.d("lifecycle","onStart invoked");
}
@Override
protected void onResume() {
super.onResume();
Log.d("lifecycle","onResume invoked");
}
@Override
protected void onPause() {
super.onPause();
Log.d("lifecycle","onPause invoked");
}
@Override
protected void onStop() {
super.onStop();
Log.d("lifecycle","onStop invoked");
}
@Override
protected void onRestart() {
super.onRestart();
Log.d("lifecycle","onRestart invoked");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d("lifecycle","onDestroy invoked");
}
}
No comments:
Post a Comment