The following two tabs change content below.
Hi, I have written and developed this site to share my experience and ideas with other colleagues. I also started to prepare interview questions and answers for job seekers. I hope it will help you a lot.

Every programming and have an entry point for example Java language has main function to launch java application.
Similar way activity also has own lifecycle which are called activity callback function.

The activity lifecycle/callback methods are:

onCreate()
Called when the activity is first created. This is where you should do all of your normal static set up — create views, bind data to lists, and so on.

onStart()
Called just before the activity becomes visible to the user.
Followed by onResume() if the activity comes to the foreground, or onStop() if it becomes hidden.

onRestart()
Called after the activity has been stopped, just prior to it being started again.

onResume()
Called just before the activity starts interacting with the user. At this point the activity is at the top of the activity stack, with user input going to it.

onPause()
Called when the system is about to start resuming another activity. This method is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, and so on. It should do whatever it does very quickly, because the next activity will not be resumed until it returns.

onStop()
Called when the activity is no longer visible to the user. This may happen because it is being destroyed, or because another activity (either an existing one or a new one) has been resumed and is covering it.
Followed either by onRestart() if the activity is coming back to interact with the user, or by onDestroy() if this activity is going away.

onDestroy()
Called before the activity is destroyed. This is the final call that the activity will receive. It could be called either because the activity is finishing (someone called finish() on it), or because the system is temporarily destroying this instance of the activity to save space.

Lifecycle flow:

lifecycle

Example of activity lifecycle:

package com.kpblog.activity;

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

public class MainActivity extends Activity {

    String msg = "Activity Callback : ";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(msg, " onCreate() event");
    }

    /** Called when the activity is about to become visible. */
    @Override
    protected void onStart() {
        super.onStart();
        Log.d(msg, " onStart() event");
    }

    /** Called when the activity has become visible. */
    @Override
    protected void onResume() {
        super.onResume();
        Log.d(msg, " onResume() event");
    }

    /** Called when another activity is taking focus. */
    @Override
    protected void onPause() {
        super.onPause();
        Log.d(msg, " onPause() event");
    }

    /** Called when the activity is no longer visible. */
    @Override
    protected void onStop() {
        super.onStop();
        Log.d(msg, " onStop() event");
    }

    /** Called just before the activity is destroyed. */
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(msg, " onDestroy() event");
    }
}

 

1,251 total views, 2 views today

Leave a Reply

Your email address will not be published. Required fields are marked *