Pages

Wednesday, May 4, 2016

Android Activity Lifecycle State change Example








MainActivity.java
package kantapp.lifecycle;


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d("kantapp","onCreate ......................................................  ");

    }

    @Override
    protected void onStart()
    {
        super.onStart();
        Log.d("kantapp","onStart ......................................................  ");

    }

    @Override
    protected void onResume()
    {
        super.onResume();
        Log.d("kantapp","onResume ......................................................  ");

    }

    @Override
    protected void onPause()
    {
        super.onPause();
        Log.d("kantapp","onPause ......................................................  ");
    }

    @Override
    protected void onStop()
    {
        super.onStop();
        Log.d("kantapp","onStop ......................................................  ");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("kantapp","onPause ......................................................  ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("kantapp","onDestroy ......................................................  ");
    }
}


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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="kantapp.lifecycle.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />
</RelativeLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="kantapp.lifecycle">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Complete Project 


Only Source Code 

No comments:

Post a Comment