Android activity example

This tutorial explains what is activity in Android, its life cycle and example.

Android activity

One of the fundamental building block of Android app development is Activity. It is just like a single screen with user interface. ContextThemeWrapper is the super class of Android activity. Activity is the main entry point of an Android app just like main method of a program written in Java or C++. An activity interacts with user so it creates a window to place UI elements. An Android application can contain several activities means many different screens that can interact with each other.

Activity life cycle

Android activity has its own life cycle during its life in Android app. Activities are stored and managed in a stack called activity stack. New activity comes at the top of stack and becomes running, while previous remains below to the new activity in stack. When the top activity in the stack exits, the lower to the top becomes active.




States of Android activity

An activity has four states

Running: Activity at the top of the stack is in running or active state means it is at the foreground of the screen.

Paused: An activity which is alive but not focused is in the paused state. (That is as a pop-up message shown on the activity).

Resumed: When a paused activity becomes active it is refreshed and started again.

Stopped: An activity which is no longer visible on the screen is in the stopped state.

Call back methods of Android activity

An Android activity implements following call back methods

OnCreate(): This is first call back method of an Android activity. It is called when an activity is created. This is where all you set all stuffs like button and views.

OnStart(): It is second call back. It is called when the activity is visible on the screen.

OnResume(): This is called after OnStart() if the activity comes to foreground. This is when user starts interacting with the activity.

OnPause(): This is followed by OnResume(). This is called when a previous activity is resumed.

OnStop(): It is called when another activity is resumed then this activity is no longer visible. It becomes hidden.

OnDestroy(): This is the final call back when the activity is finished.

The following diagram shows complete life cycle of an Android activity.

Android activity life cycle

Android activity life cycle

Rectangles in the figure represent the 6 call back methods.

Android activity example

Here is the example of Android activity. Open your Android Studio and create a new project. New project wizard will allow you to create a new  activity. You can find more info on how to create a new Android project in “Creating a Simple android App” tutorial

package com.example.admin.androidactivity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class ActivityExample extends AppCompatActivity {

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


}

Here is the example code, you can download.

0 0 votes
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments