Create an android app that demonstrates Activity Lifecycle and Instance State.

In this tutorial we will see the working of Activity lifecycle step by step. Activity  life cycle consists of 7 methods as below.





  1. onCreate() -   Invoked when Activity is created.
  2. onStart() -      Invoked when Activity is becoming visible.
  3. onResume() - Invoked when Activity starts Interacting with user.
  4. onPause() -    Invoked when Activity is invisible to user
  5. onStop() -       Invoked when activity is no longer visible.
  6. onRestart() -  Invoked after your activity is stopped, prior to start.
  7. onDestroy() - Invoked  before Activity is killed.
Let's begin coding....πŸ’»πŸ’»







Step 1: After creating Android project, open activity_main.xml file and paste below code as it is.

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>  
<android.support.constraint.ConstraintLayout 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"  
    tools:context="example.javatpoint.com.activitylifecycle.MainActivity">  
      <TextView  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="Hello World!"  
        app:layout_constraintBottom_toBottomOf="parent"  
        app:layout_constraintLeft_toLeftOf="parent"  
        app:layout_constraintRight_toRightOf="parent"  
        app:layout_constraintTop_toTopOf="parent" />    
</android.support.constraint.ConstraintLayout>

Step 2: Now open MainActivity.java file and paste below code.

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

public class MainActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toast.makeText(MainActivity.this,"onCreate invoked",Toast.LENGTH_SHORT).show();

        Log.d("lifecycle","onCreate invoked");
    }

    @Override
    protected void onStart()
    {
        super.onStart();
        Toast.makeText(MainActivity.this,"onStart invoked",Toast.LENGTH_SHORT).show();

        Log.d("lifecycle","onStart invoked");
    }

    @Override
    protected void onResume() 
    {
        super.onResume();
        Toast.makeText(MainActivity.this,"onResume invoked",Toast.LENGTH_SHORT).show();

        Log.d("lifecycle","onResume invoked");
    }

    @Override
    protected void onPause()
    {
        super.onPause();
        Toast.makeText(MainActivity.this,"onPause invoked",Toast.LENGTH_SHORT).show();

        Log.d("lifecycle","onPause invoked");
    }

    @Override
    protected void onStop()
    {
        super.onStop();
        Toast.makeText(MainActivity.this,"onStop invoked",Toast.LENGTH_SHORT).show();

        Log.d("lifecycle","onStop invoked");
    }

    @Override
    protected void onRestart()
    {
        super.onRestart();
        Toast.makeText(MainActivity.this,"onRestart invoked",Toast.LENGTH_SHORT).show();

        Log.d("lifecycle","onRestart invoked");
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        Toast.makeText(MainActivity.this,"onDestroy invoked",Toast.LENGTH_SHORT).show();

        Log.d("lifecycle","onDestroy invoked");
    }

}

Step 3: wohoo!! we are done with the coding in just two steps. Now run the project and see output in Logcat.

Note: Logcat is located at the bottom of the Android Studio.


Enjoy coding!!!πŸ™Œ

Comments

  1. Short and simple... Easy to understand..

    ReplyDelete
  2. Too much easy to understand and completely sorted steps, awesome one ma'am, I would like to have more blogs such like this

    ReplyDelete
  3. Mast madam...good going

    ReplyDelete
  4. Easy to understand

    ReplyDelete
  5. Yeah, easy to understand. But i think database is more suitable for me πŸ˜…πŸ˜…

    ReplyDelete
    Replies
    1. thq vedant. I will post Databse related blogs too.

      Delete
    2. Yes, thanks, I'm working on mysql, mongodb, PostgreSQL Databases.

      Delete
  6. Explained really well step by step. Keep posting such amazing tutorials ��

    ReplyDelete
  7. Good job Bhakti.Keep it up. Short and simple steps. Very easy to understand.

    ReplyDelete
  8. Very helpful content for us so thank alot...πŸ™‚

    ReplyDelete
  9. Waw very nice .. keep it up sister 😊

    ReplyDelete
  10. Explained very well in short and simple steps. Helpful content ma'am 😊

    ReplyDelete
  11. Very Helpful Content πŸ‘ŒπŸ‘Œ Keep it up SisterπŸ‘πŸ‘

    ReplyDelete
  12. Very helpful content to understand the process along with it's proper working code!!!Thank youπŸ‘

    ReplyDelete
  13. Thankyou mam... Quite clear with these concepts... πŸ‘

    ReplyDelete
  14. Very helpful content
    Thank you mam

    ReplyDelete

Post a Comment

Popular posts from this blog

Program using Light Sensitive Sensors on Tinkercad