Skip to main content

Android Activity Life Cycle

This is the most important and basic concept in Android. In Android, every activity has its on life cycle. An activity encountered the following methods in its life cycle.


1. onCreate()
2. onStart()
3. onResume()
4. onPause()
5. onStop()
6. onResume()
7. onDestroy()




1. onCreate(): This method is called only once in an activity life cycle. onCreate() method is called when an activity is created for the first time. This is the main method where we initialize all the variables.
                In onCreate() method, a variable of class Bundle is passed. This helps to save the state of our activity or application.

It comes in the role when the orientation of an activity is changed. A very basic example is, when a user is filling online form in android application and changes its orientation from portrait to landscape or vice versa then Bundle class helps the application in retaining the user data.


2. onStart(): onStart() method is called when an application or activity becomes visible to the user. It is usually used to reset activity data or re-initializing the variables. In entire life cycle of an activity, onStart() method possible be called twice i.e. after onCreate() and onRestart().


3. onResume(): Android calls onResume() method when an activity becomes visible to the user. In this state user can interact when the application.

4. onPause(): It is called when a new activity comes above the current activity. In this case current activity gets in pause state. when onPause() is called an activity becomes invisible or partially visible to the user.

Usually onPause() method is called to save the state of the current activity, so that ther ewill be no data loose when any other application interrupts your application.

5. onStop(): When onStop() method is called our application becomes invisible to the user. it is also called to save the current state of application. 

There is a thin line between onPasue() and onStop(). when another activity comes to the foreground then our activity is in Pause and Stop both the states. but when any text message or any alert message appears on screen then it becomes partially visible and called onPause() method.

6. onRestart(): This is quite similar to the onCreate() method. But we usually called this method to see our hidden activity once again. After calling onStop(), android adds the activity into the activity stack and another activity comes in to the foreground. But when user intentionally calls up any activity which is in activity stack, then onRestart() method is called.

7. onDestroy(): On calling onDestroy() method, the activity gets killed. This could be possible in only two cases. one if user shutdowns the entire application and second when user manually calls "finish()" method.

Once the onDestory method comes into role then for starting the application android has to start the entire life cycle from beginning.

  


Comments

Popular posts from this blog

Android: Login Screen using Fragments

In this tutorial, our focus is on making a User/Member login activity using fragments. With the help of Fragments we will use the same activity to show User login area and also Members Login area. Prerequisite for this tutorial: You should be know how to make an Activity And most importantly you should have prior Knowledge of Fragments. For practising basic Fragment implementation refer to Android Simple Fragment Example      We have used only three activities for this: MainActivity(which represents the Login Screen) Fragments for Members area Fragment for New Users

Simple Login/Register Example using SQLite database

MainActivity.java(login Screen) package com.AndroidDevelopmentGuru.database_new; import java.util.List; import android.app.Activity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity {                                 EditText user, pass;                 Button login, not_reg;                 DatabaseHandler db;                 Cursor cursor;                                 @Override                 protected void onCreate(Bundle savedInstanceState) {                                 super.onCreate(savedInstanceState);                                 setContentView(R.layout.activity_main);

Android: Current Location Using Fused APi on Google Maps

This tutorial gives us the simple implementation of "Fused API" to fetch the current location on google map in android. Fused API is latest among all techniques to get the location. It provides you very precise results and also uses less battery of your device. It chooses GPS or Network provider to get to your current location. And it helps your device remember about the last saved location. Let's implement the Fused API to fetch/get the current location. Step 1: Create a new project in Android studio. and select Maps Activity.