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
Created
Created state is temporary. The system moves to the next state immediately.
Started
Started state is also a temporary state. The system moves to the next state immediately.
Resumed
This state is also known as running state. The activity becomes the foreground activity. The user can interact with the resumed i.e. running activities.
Paused
This 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.
Stopped
In 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 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 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // The activity is being created. } |
onStart()
1 2 3 4 |
@Override public void onStart() { super.onStart(); // The activity is about to become visible. } |
onResume()
1 2 3 4 |
@Override public void onResume() { super.onResume(); // The activity has become visible (it is now "resumed"). } |
onPause()
1 2 3 4 |
@Override public void onPause() { super.onPause();// Another activity is taking focus. } |
onStop()
1 2 3 4 |
@Override public void onStop() { super.onStop();// The activity is no longer visible (it is now "stopped") } |
onDestroy()
1 2 3 4 |
@Override public void onDestroy() { 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.
Syntax
1 2 3 |
String url="http://google.com"; // string resource for implicit intent Intent intent=new Intent(Intent.ACTION_VIEW,Uri.parse(url)); //implicit intent instance startActivity(intent); //starting an implicit intent |
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=new Intent(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=new Intent(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.
1 2 3 |
Intent intent=getIntent(); //instance of intent String message1=intent.getStringExtra("value1"); // receive first string String message2=intent.getStringExtra("value2"); //receive second string |
Start Second Activity for Result
1 2 |
Intent intent=new Intent(this, SecondActivity.class); //create a new intent for the second/next activity startActivityForResult(intent, 2); //start activity for result |
Send Results from Next/Second Activity
1 2 3 4 5 6 7 8 |
String message=editText1.getText().toString(); //message from a text box or you can also set string manually Intent intent=new Intent(); //create instance of intent intent.putExtra("MESSAGE",message); //put extra message in intent setResult(2,intent); //set result and number of results finish(); //finish an activity |
Receive Result in the Previous Activity
1 2 3 4 5 6 7 8 9 10 11 |
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // check if the request code is same as what is passed if(requestCode==2) { String message=data.getStringExtra("MESSAGE"); } } |
Read more about Android Activity