Activity Life Cycle/Activity life cycle example in android
- An activity represents a single screen with a user interface
- A users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map
- An activity is implemented as a subclass of Activity
- An application usually consists of multiple activities that are loosely bound to each other
Example for Activity LifeCycle
LifeCycleActivity .java
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(this, "onCreate()", Toast.L E N G TH_LO N G ).show();
}
@Override
protected void onStart() {
//the activity is become visible.
super.onStart();
Toast.makeText(this, "onStart()", Toast.L E N G TH_LO N G ).show();
}
@Override
protected void on P ause() {
super.onPause();
Toast.makeText(this, "on P ause()", Toast.L E N G TH_LO N G ).show();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super .onResume();
Toast.makeText(this,” onResume() ” , Toast.LENGTH_LONG).show();
}
@Override
protected void onStop() {
//the activity is no longer visible.
super.onStop();
Toast.makeText(this, "onStop()", Toast.L E NGTH_LONG).show();
}
@Override
protected void onDestroy() {
//The activity about to be destroyed.
super.onDestroy();
Toast.makeText(this, "onDestroy()", Toast.L E NGTH_LONG).show();
}
}
main.xml
<?xml version= “ 1 . 0 " encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="v e r tical" android:layout_width="fi Ll _parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:text="button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="true" android:onclick="select"> </Button> </LinearLayout> |
Comments
Post a Comment