Wednesday, 26 August 2015

Android Programming Tutorials 15 : Activity LifeCycle Methods In Android

Activity LifeCycle Methods:

In this tutorial we are discussing how the methods of Activity Lifecycle is executing.




About Activity LifeCycle we have discussed long back just refere my tutorial.

Create Android Application:
  • ·      File >> New >> Android Application
    ·         Enter Project name: ActivityLifeCyCleMethodsApp
    ·         Package: com.ambilpursunil.newapp
    ·         Keep other default selections, click Next until you reach Finish


1.Simply Copy and Past the Code which is display below in the    MainActivity.java

package com.example.lifecyclemethodsofactivity;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_main);
                        display("We are in the OnCreate()");
             }

            @Override
            public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
                        getMenuInflater().inflate(R.menu.main, menu);
                        return true;
            }

            @Override
            protected void onStart() {
                        // TODO Auto-generated method stub
                        super.onStart();
                        display("We are in the OnStart()");
            }

            @Override
            protected void onResume() {
                        // TODO Auto-generated method stub
                        super.onResume();
                        display("We are in the OnResume()");
                        finish();
            }

            @Override
            protected void onRestart() {
                        // TODO Auto-generated method stub
                        super.onRestart();
                        display("We are in the OnRestart()");
            }

            @Override
            protected void onPause() {
                        // TODO Auto-generated method stub
                        super.onPause();
                        display("We are in the OnPause()");
            }

            @Override
            protected void onStop() {
                        // TODO Auto-generated method stub
                        super.onStop();
                        display("We are in the OnStop()");
            }

            @Override
            protected void onDestroy() {
                        // TODO Auto-generated method stub
                        super.onDestroy();
                        display("We are in the OnDestroy()");
            }

            public void display(String msg) {
Toast.makeText(getBaseContext(), msg, Toast.LENGTH_LONG).show();
            }

}


2.Simple Copy and Past the below code:  actvitiy_main.xml

<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="com.example.lifecyclemethodsofactivity.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

3.Copy and past the code for  manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.contentproviderbydarekbanas"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            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>

5.Right click on the project and Run As-->Android Application


OutPut:
See the Output it will dispaly in the form of Toast message, which methods are called. First onCreate(), onStart() and onResume() methods wil execute if you interact with the activity screen onPause() and onResume() methods will called if you click on back or exit button onStop() and onDestroy() methods will called.

Please Send Your Comments To ambilpura.sunil@gmail.com

Stay Tune For Next Tutorial... Fragments In Android:

No comments:

Post a Comment