Activity Lifecycle

Activity Lifecycle State change  Example in Android

Class File

package com.example.malik.test;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends Activity {

public static final String MY_TAG = “My_Custom_Message”;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(MY_TAG, “onCreate Message”);
}

@Override
protected void onStart() {
super.onStart();
Log.i(MY_TAG, “onStart Message”);
}

@Override
protected void onResume() {
super.onResume();
Log.i(MY_TAG, “onResume Message”);
}

@Override
protected void onPause() {
super.onPause();
Log.i(MY_TAG, “onPause Message”);
}

@Override
protected void onStop() {
super.onStop();
Log.i(MY_TAG, “onStop Message”);
}

@Override
protected void onRestart() {
super.onRestart();
Log.i(MY_TAG, “onRestart Message”);
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.i(MY_TAG, “onDestroy Message”);
}
}

 

XML FILE

 

<?xml version=”1.0″ encoding=”utf-8″?>
<LinearLayout 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:orientation=”vertical”
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=”com.example.malik.test.MainActivity”>
<TextView
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:textAppearance=”?android:attr/textAppearanceLarge”
android:text=”Hello World”
android:id=”@+id/textView”
android:layout_gravity=”center_horizontal” />
</LinearLayout>

 

Output :

pic pic2

 

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.