• Techiax

Tutorial - Android Activity Lifecycle





Activity is the first important component you should learn to get into the area of Android App Development. For a short and better understanding, An Activity is a single screen with different components like buttons, text views, etc. you see in an android app. You would have seen in many android apps that when you interact with the screen like pressing buttons or entering text then the screen would change into a new one, this is because you are moving from one activity to another activity, each screen corresponds to one activity. Now let's get into activity.





Also Read: Best softwares(IDE) for Android apps development

Activity Lifecycle:

The first thing you got to know about activity is its life cycle and the methods for Lifecycle.

onCreate()

    onCreate() method is called when the activity is first created like opening the app from menu, opened after the memory for that activity is destroyed upon last use of the activity. This method should be created compulsory for an activity.

onStart()

    onStart() method continues after onCreate() method is finished performing. It immediately follows after onCreate(), so you would hardly recognise the change. It is called after performing onRestart().

onResume()

    onResume() is called after onStart() is finished or when the user returns to the activity from the onPause state. It is used to resume the activity.


onPause()

     onPause() method is called when the user makes the activity go to the foreground by starting another activity. And when the user returns onResume() method is called.

onStop()

      onStop() method is called when the activity becomes no longer visible to the user. This happens mostly on situations like pressing the menu button and opening another app. And when the user returns onRestart() method is called.

onRestart()

       onRestart() method is called when the user returns to the app after it was stopping by onStop() method.

onDestroy()

        onDestroy() method is called when there is no use for the activity and the activity with its memory is being destroyed by operating system. After this stage, there will be nothing to do with the app.

This diagram would clearly state the flow of controls in different states.

Image source: developer.android.com


Upon the above methods, onCreate() method is compulsory to include in your activity.
Others are optional and if you didn't include it in your activity, the android operating system creates the methods for you. That doesn't mean you don't need to worry about these methods, the lifecycle is important to many situations if you don't want your app to crash when users press menu button or other make interactions.

Also Read: Disable unwanted Windows startup programs to fasten your system

Example:

Open a new app for creation and create a new activity called MainActivity and insert the following codes.

In this example, you won't needed to edit AndroidManifest.xml file if you are using an IDE or else add this code into your manifest file.


AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.techiax" >

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>




MainActivity.java

 package com.example.techaix;
 import android.support.v7.app.ActionBarActivity;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.Menu;
 import android.view.MenuItem;


 public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("Status","onCreate is happening");
    }


    @Override
    protected void onStart() {
        super.onStart();
        Log.d("Status","onStart is happening");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d("Status","onResume is happening");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d("Status","onPause is happening");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d("Status","onStop is happening");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d("Status","onRestart is happening");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("Status","onDestroy is happening");
    }
 }



activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context=".MainActivity">

    <TextView
        android:text="Techiax"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAlignment="center"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/techiax"
        android:textIsSelectable="true"
        android:textSize="40sp"
        android:textStyle="bold"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Activity tutorial"
        android:id="@+id/textView"
        android:layout_marginTop="102dp"
        android:textSize="30sp"
        android:layout_below="@+id/techiax"
        android:layout_centerHorizontal="true"/>

 </RelativeLayout>

Logcat Outputs:


You will see different outputs at different interactions with the activity.

When the you open the app for first time or after removing from memory

 12-27 12:38:39.350  29238-29238/com.example.techaix D/Status﹕ onCreate is happening
 12-27 12:38:39.353  29238-29238/com.example.techaix D/Status﹕ onStart is happening
 12-27 12:38:39.358  29238-29238/com.example.techaix D/Status﹕ onResume is happening


If you click menu button

 12-27 12:39:23.423  29238-29238/com.example..techaix D/Status﹕ onPause is happening
 12-27 12:39:27.573  29238-29238/com.example.techaix D/Status﹕ onStop is happening

When returning to app from recent apps menu

 12-27 12:39:50.258  29238-29238/com.example.techaix D/Status﹕ onRestart is happening
 12-27 12:39:50.281  29238-29238/com.example.techaix D/Status﹕ onStart is happening
 12-27 12:39:50.281  29238-29238/com.example.techaix D/Status﹕ onResume is happening


Be sure to make different kinds of interactions with your app and note the methods called and know how to use it to the advantage of your app.



Please share your views and queries. This will make us do more for you.






Author:

I am an enthusiastic person striving to gather as much information as possible to achieve higher heights and to share the information to all others to make them also reach the stars.
"People who are crazy enough to think they can change the world are the ones who do"
-Steve Jobs.

Previous Post
Next Post