Home » Android

Android Activity Lifecycle

In this article, we will learn about Android Activity Life Cycle. How the activity behaves in different states?
Submitted by Shamikh Faraz, on January 20, 2018

Activity Lifecycle contains 7 methods of ‘Activity’ class. These 7 methods will define how an activity functions on different states. The activity is the subclass of ContextThemeWrapper class. Activity is also used to place UI components/widgets on a single window. In general, an activity is a screen/window in android.

Android Activity Lifecycle methods

  1. onCreate(): This is called when an activity creates.
  2. onStart(): This is called when an activity becomes visible to the user.
  3. onResume(): This is called when the user starts interaction with app.
  4. onPause(): The is called when an activity does not receive any input from user and cannot execute any code and is called when the previous activity is being resumed as well.
  5. onStop(): This is called when the activity is no longer visible to user.
  6. onRestart(): This is called when the activity restarts after stopping it.
  7. onDestroy(): This is called before the activity is destroyed by the system/user.

The onCreate() and onDestroy() methods are called only once throughout the activity lifecycle.

Android Activity lifecycle

XML File: (activity_main)

<?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="com.example.faraz.activitylifecycle_example.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>

JAVA File: (MainActivity.java)

package com.example.faraz.activitylifecycle_example;

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

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("lifecycle","onCreate called");
    }
    @Override
    protected void onStart() {
        super.onStart();
        Log.d("lifecycle","onStart called");
    }
    @Override
    protected void onResume() {
        super.onResume();
        Log.d("lifecycle","onResume called ");
    }
    @Override
    protected void onPause() {
        super.onPause();
        Log.d("lifecycle","onPause called ");
    }
    @Override
    protected void onStop() {
        super.onStop();
        Log.d("lifecycle","onStop called ");
    }
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("lifecycle","onRestart called ");
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("lifecycle","onDestroy called ");
    }
}

Output: Emulator or your device can not provide output. For output, you have to go to ‘logcat’. Click Run icon from the toolbar, it will only display emulator, but output in LogCat window:

1) When you start your app the following methods are called:

    onCreate()
    onStart()
    onResume()

2) When your app goes to the background by opening another app the following methods are called:

    onPause()
    onStop()

3) When you go back to your app the following methods will be called:

    onRestart()
    onStart()
    onResume()

4) When you permanently close your app the methods are called as below:

    onPause()
    onStop()
    onDestroy()





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.