Sunday, 16 September 2018

Android Activity Life Cycle Example

Create New project ActivityLifeCycle with empty activity.

// activity_main.xml
<?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:id="@+id/activity_main" 
   android:layout_width="match_parent"
    android:layout_height="match_parent" 
   android:orientation="vertical" 
   tools:context="com.krishanandroid.activitylifecycle.MainActivity">

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

    <Button      
  android:layout_width="match_parent"   
     android:layout_height="wrap_content"  
      android:text="Open Second Screen"  
      android:id="@+id/button"  
      android:onClick="openSecondScreen"/>
</LinearLayout>

// MainActivty.Java
package com.krishanandroid.activitylifecycle;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

        Toast.makeText(MainActivity.this,"1. onCreate",Toast.LENGTH_LONG).show();
    }

    @Override    protected void onStart() {
        super.onStart();
        Toast.makeText(MainActivity.this,"2. onStart",Toast.LENGTH_LONG).show();
    }

    @Override    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        Toast.makeText(MainActivity.this,"3. onRestore",Toast.LENGTH_LONG).show();
    }

    @Override    protected void onResume() {
        super.onResume();
        Toast.makeText(MainActivity.this,"4. onResume",Toast.LENGTH_LONG).show();
    }

    @Override    protected void onPause() {
        super.onPause();
        Toast.makeText(MainActivity.this,"5. onPause",Toast.LENGTH_LONG).show();
    }

    @Override    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Toast.makeText(MainActivity.this,"6. onSave",Toast.LENGTH_LONG).show();
    }

    @Override    protected void onStop() {
        super.onStop();
        Toast.makeText(MainActivity.this,"7. onStop",Toast.LENGTH_LONG).show();
    }

    @Override    protected void onRestart() {
        super.onRestart();
        Toast.makeText(MainActivity.this,"8. onRestart",Toast.LENGTH_LONG).show();
    }

    @Override    protected void onDestroy() {
        super.onDestroy();
        Toast.makeText(MainActivity.this,"9. onDestroy",Toast.LENGTH_LONG).show();
    }

    // On button click implementation    public void openSecondScreen(View view) {

        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        startActivity(intent);
    }
}

// activity_second.xml
<?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:id="@+id/activity_second" 
   android:layout_width="match_parent" 
   android:layout_height="match_parent" 
   android:orientation="vertical"  
  tools:context="com.krishanandroid.activitylifecycle.SecondActivity">

    <TextView     
   android:layout_width="match_parent"   
     android:layout_height="wrap_content" 
       android:text="Welcome to Second Screen"  
      android:textColor="@color/colorAccent"   
     android:textSize="24dp"/>

</LinearLayout>

// SecondActivity.java
package com.krishanandroid.activitylifecycle;

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

public class SecondActivity extends AppCompatActivity {

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
    }
}
Live Demo: https://www.youtube.com/watch?v=PYnBSDMsO2c&t=121s


No comments:

Post a Comment