Android Activities

Introduction to Android Activities

Activity, as previously discussed in User Interface in Android, is a single screen for user interaction. Activity takes input from the user and gives output to the user. Android Activity is composed of views and view groups. An Application has single or multiple Android activities. Activities are loosely bound to each other, which means that it is not necessary for any activity to have any prerequisite activity. Any activity can start another activity. The only restriction in the case of activities is that only one activity can run at a time, the previous activity must stop before starting another activity.

Working of Android Activities

When any activity starts, it is pushed into the back stack and gets the focus on the screen. The back stack strictly follows LIFO (Last in, First out) i.e. stack mechanism. Whenever the user presses the back button current activity is popped out from the stack and destroyed and the previous activity resumes.

Lifecycle of Android Activity

When any activity stops because a new activity starts, the state changes and is notified through activity call back methods. There are several call back methods that an activity receives when its state change i.e. when the system is creating it, stopping it, resuming it or destroying it.

Lifecycle States of Android Activity

CreatedCreated state is temporary. The system moves to the next state immediately.

StartedStarted state is also a temporary state. The system moves to the next state immediately.

ResumedThis state is also known as running state. The activity becomes the foreground activity. The user can interact with the resumed i.e. running activities.

PausedThis state tells that the activity is temporarily stopped or the activity become semi-transparent i.e. it does not cover the entire screen. The user can not interact with the activity in the paused state.

StoppedIn the Stopped state the activity is completely hidden and the user can not see it. The stopped activity is running in the background as the instance of the activity, its state information i.e. member variables and functions are retained, and it can’t perform any task.

There are some situations in which the activity can switch between states.

The Activity is in the stopped, paused and resumed state. Only these three states are static. The activity can exist between these states for a longer period.

The other States i.e. created and started are transient (temporary), and the system quickly moves from these states to the next state. The changing of state can be done by calling the next lifecycle callback method.

Android Activity Lifecycle (This image is taken from official Android developers documentation)

Android Callback methods

onCreate()

onRestart()

onStart()

onResume()

onPause()

onStop()

onDestroy()

Implementing Callback Methods in Android Activities

All the callback methods are implemented by overriding them. They are appropriately called when the state of the activity changes. You must call the superclass method’s implementation before performing any task.

onCreate()

1

2

3

4

@Override

publicvoidonCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);// The activity is being created.

}

onStart()

1

2

3

4

@Override

publicvoidonStart(){

super.onStart();// The activity is about to become visible.

}

onResume()

1

2

3

4

@Override

publicvoidonResume(){

super.onResume();// The activity has become visible (it is now "resumed").

}

onPause()

1

2

3

4

@Override

publicvoidonPause(){

super.onPause();// Another activity is taking focus.

}

onStop()

1

2

3

4

@Override

publicvoidonStop(){

super.onStop();// The activity is no longer visible (it is now "stopped")

}

onDestroy()

1

2

3

4

@Override

publicvoidonDestroy(){

super.onDestroy();// The activity is about to be destroyed.

}

These methods must be implemented after overriding and implementing onCreate() method within the activity.

To see when these callback methods are called during Activity’s lifecycle by using Log utility.

1

Log.d("TAG","Message");

Intent

When you want to request any action from another app component or you, want to pass any message to another app component you can use intent. There are two types of intents.

Implicit intents

Explicit intents

Implicit Intent

Implicit intent does not call any specific app component; it generates a general action to be performed. This allows any other app’s component to handle the request. For example, if you want to use the camera, you need to call an implicit intent that will automatically call appropriate camera app to handle the request.

Explicit Intent

Explicit intent calls a specific app component to start by name i.e. Fully qualified class name. For Example, if you want to call any component of your app and you know the class name of any service or activity you want to start.

Syntax

1

2

Intent intent=newIntent(this,SecondActivity.class);//create intent for next activity

startActivity(intent);//start second activity

Passing extra message in intents

Sending extra message through intent

You can also pass an extra message to your intent for the next activity.

1

2

3

4

Intent intent=newIntent(this,SecondActivity.class);//intent to start second activity

intent.putExtra("value1","Some Value");//put extra message in intent for next activity

intent.putExtra("value2","Another Value");//put another extra message in intent for next activity

startActivity(intent);//start second/next activity

Receiving Extra Message through Intent

In the newly started activity, you can get an extra message which was sent by the previous activity.