Skip to main content

Android Life Cycle of Activity

We can control and manage the resource through the life cycle methods of activity.
There are 7 life cycle methods of android.app.Activity class. They are as follows:
MethodDescription
onCreatecalled when activity is first created.
onStartcalled when activity is becoming visible to the user.
onResumecalled when activity will start interacting with the user.
onPausecalled when activity is not visible to the user.
onStopcalled when activity is no longer visible to the user.
onRestartcalled after your activity is stopped, prior to start.
onDestroycalled before the activity is destroyed.
android activity lifecycle

Android Activity Life Cycle Example

It provides the details about the invocation of life cycle methods of activity. In this example, we are displaying the content on the logcat.
File: MainActivity.java
  1. package com.example.activitylifecycle;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.util.Log;  
  6. import android.view.Menu;  
  7.   
  8. public class MainActivity extends Activity {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_main);  
  14.         Log.d("lifecycle","onCreate invoked");  
  15.     }  
  16.     @Override  
  17.     protected void onStart() {  
  18.         super.onStart();  
  19.          Log.d("lifecycle","onStart invoked");  
  20.     }  
  21.     @Override  
  22.     protected void onResume() {  
  23.           
  24.         super.onResume();  
  25.          Log.d("lifecycle","onResume invoked");  
  26.     }  
  27.       
  28.   
  29.     @Override  
  30.     protected void onPause() {  
  31.           
  32.         super.onPause();  
  33.          Log.d("lifecycle","onPause invoked");  
  34.     }  
  35.     @Override  
  36.     protected void onStop() {  
  37.           
  38.         super.onStop();  
  39.          Log.d("lifecycle","onStop invoked");  
  40.     }  
  41.       
  42.        @Override  
  43.     protected void onRestart() {  
  44.           
  45.         super.onRestart();  
  46.          Log.d("lifecycle","onRestart invoked");  
  47.     }     
  48.     @Override  
  49.     protected void onDestroy() {  
  50.           
  51.         super.onDestroy();  
  52.          Log.d("lifecycle","onDestroy invoked");  
  53.     }  
  54. }  


Output:

You will not see any output on the emulator or device. You need to open logcat.
android activity life cycle example output 1
Now see on the logcat: onCreate, onStart and onResume methods are invoked.
android activity life cycle example output 2
Now click on the HOME Button. You will see onPause method is invoked.
android activity life cycle example output 3
After a while, you will see onStop method is invoked.
android activity life cycle example output 4
Now see on the emulator. It is on the home. Now click on the center button to launch the app again.
android activity life cycle example output 5
Now click on the lifecycleactivity icon.
android activity life cycle example output 6
Now see on the logcat: onRestart, onStart and onResume methods are invoked.
android activity life cycle example output 7
If you see the emulator, application is started again.
android activity life cycle example output 8
Now click on the back button. Now you will see onPause methods is invoked.
android activity life cycle example output 9
After a while, you will see onStop and onDestroy methods are invoked.
android activity life cycle example output 10

Comments

Popular posts from this blog

Android Tutorial

Android  is a complete set of software for mobile devices such as tablet computers, notebooks, smartphones, electronic book readers, set-top boxes etc. It contains a  linux-based Operating System ,  middleware  and  key mobile applications . It can be thought of as a mobile operating system. But it is not limited to mobile only. It is currently used in various devices such as mobiles, tablets, televisions etc. This tutorial is developed for beginners and experienced persons. Let's see the topics of android that we are going to learn. Basics of Android In this fundamental chapter, you will learn about android, its components, how to create first android application, internal of first android application etc. What is Android History and Version Software Stack Core Building Blocks Android Emulator Installing softwares Setup Eclipse Hello Android example Internal Details Dalvik VM AndroidManifest.xml R.java Hide Title Bar Activity and I...

CLOUD WORKFLOW SCHEDULING WITH DEADLINE AND TIME SLOT ALGORITHM

CLOUD WORKFLOW SCHEDULING WITH DEADLINE AND TIME SLOT ALGORITHM Abstract Allocating service capacities in cloud computing is based on the assumption that they are unlimited and can be used at any time. However, available service capacities change with workload and cannot satisfy users’ requests at any time from the cloud provider’s perspective because cloud services can be shared by multiple tasks. Cloud service providers provide available time slots for new user’s requests based on available capacities. In this paper, we consider workflow scheduling with deadline and time slot availability in cloud computing. An iterated heuristic framework is presented for the problem under study which mainly consists of initial solution construction, improvement, and perturbation. Three initial solution construction strategies, two greedy- and fair-based improvement strategies and a perturbation strategy are proposed. Different strategies in the three phases result in several heuristics. ...

MobiContext: A Context-aware Cloud-Based Venue Recommendation Framework

            MobiContext: A Context-aware Cloud-Based Venue Recommendation Framework ABSTRACT  In recent years, recommendation systems have seen significant evolution in the field of knowledge engineering. Most of the existing recommendation systems based their models on collaborative filtering approaches that make them simple to implement. However, performance of most of the existing collaborative filtering-based recommendation system suffers due to the challenges, such as: (a) cold start, (b) data sparseness, and (c) scalability. Moreover, recommendation problem is often characterized by the presence of many conflicting objectives or decision variables, such as users’ preferences and venue closeness. In this paper, we proposed MobiContext , a hybrid cloud-based Bi-Objective Recommendation Framework (BORF) for mobile social networks. The MobiContext utilizes multi-objective optimization techniques to generate personalized recommendat...