Here you'll get help for all programming languages like android,php, html, css, bootstrap, javaScript, java etc...

Android Lifecycle Method

Here we are going to learn  lifecycle methods in android.

In Android Activity is controlled by 7 lifecycle methods.

If you have worked with C or C++ or Java programming  then you must have seen that your program starts with main() function.

Main is the starting point of your program.

So, in android when activity opens your onCreate method will invoke first.

We have 7 lifecycle methods in android


  • onCreate
  • onStart  
  • onResume
  • onPause
  • onStop
  • onRestart
  • onDestroy


onCreate  methods called when activity is first opened or created.

onStart  method called when activity is visible to the user.

onResume  method called when user start interacting with the activity.

onPause method called when activity is not visible to the user.

onStop method called when activity is not  visible to the user.

(Both onPause and onStop method call at the same time)

onRestart method called when user restart activity.

onDestroy method called when activity destroyed.






Let's see example ::







activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#589"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="150dp"
        android:text="         Android \nLifeCycle Methods"
        android:textSize="32sp"
        />

</LinearLayout>



MainActivity.java


package com.example.malik.blog;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;


public class MainActivity extends AppCompatActivity {

    public static final String USER_TAG = "USER_TAG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.e(USER_TAG, "onCreate Method Call");
    }


    @Override
    protected void onStart() {
        super.onStart();
        Log.e(USER_TAG, "onStart Method Call");
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.e(USER_TAG, "onResume Method Call");
    }


    @Override
    protected void onPause() {
        super.onPause();
        Log.e(USER_TAG, "onPause Method Call");
    }


    @Override
    protected void onStop() {
        super.onStop();
        Log.e(USER_TAG, "onStop Method Call");

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e(USER_TAG, "onRestart Method Call");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(USER_TAG, "onDestroy Method Call");
    }

}



AndroidManifest.xml


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

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

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

</manifest>



build.gradle


apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.malik.blog"
        minSdkVersion 18
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}













User Help

If you want any source code or any other help just contact me at malik_himani@ymail.com. Your requirement's code will be uploaded in blog soon.