An activity is a single screen with a user
interface. It is a mean of providing user interaction with your
application. Activity class takes care of creating a window for you in
which you can place your UI with setContentView(View). There is one
method almost all subclasses of Activity will implement:
- onCreate(Bundle) is where you initialize your
activity. You will usually call setContentView(int) with a layout
resource defining your UI, and using findViewById(int) to retrieve the
widgets in that UI that you need to interact with programmatically.
Activity Lifecycle
The following diagram shows the important state paths of an
Activity. By understanding the paths, you know where you should
place your code.
- onCreate() Called when the activity is first created. This is
where you should do all of your normal static set up: create views, bind
data to lists, etc.
- onRestart() Called after your activity has been stopped, prior to it
being started again.
- onStart() Called when the activity is becoming visible to the user.
- onResume() Called when the activity will start interacting with the
user. At this point your activity is at the top of the activity stack,
with user input going to it.
- onPause() Called when the system is about to start resuming a previous
activity. This is typically used to commit unsaved changes to persistent
data, stop animations and other things that may be consuming CPU, etc.
- onStop() Called when the activity is no longer visible to the user,
because another activity has been resumed and is covering this one. This
may happen either because a new activity is being started, an existing
one is being brought in front of this one, or this one is being
destroyed.
- onDestroy() Called before your activity is destroyed. This can happen
either because the activity is finishing by calling finish() on it, or
because the system is temporarily destroying this instance of the
activity to save space.
Example
In this example, you will better understand the state paths of
an activity. We have two activities, MainActivity and SecondActivity. On
the MainActivity, there is a button to open the SecondActivity. By
trying to switch between the activities using the button and back press,
you see a method that is called first and called next.
- Create a new Android project using Android Studio. Name the project as
ActivityLifecycleExample.
- In the activity_main.xml file, you add a Button view.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android= "http://schemas.android.com/ apk/res/android"
xmlns:tools= "http://schemas.android.com/tools"
android:id= "@+id/activity_main"
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= "com.example.dara. activitylifecycleexample.MainActivity">
<Button
android:layout_width= "wrap_content"
android:text= "Start Second Activity"
android:onClick= "startSecondActivity"
android:layout_height= "wrap_content" />
</RelativeLayout>
Once an activity is created, a layout file is automatically created for
you. The Main activity has activity_main.xml layout file. In the layout
file, you can define UI element of the Main Activity.
- In the MainActivity class, you write code as shown below:
package com.example.dara. activitylifecycleexample;
import android.content.Intent;
import android.support.v7.app. AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("MainActivity","onCreate called");
}
public void startSecondActivity(View v){
Intent intent=new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
protected void onStart(){
super.onStart();
Log.i("MainActivity","onStart called");
}
protected void onRestart(){
super.onRestart();
Log.i("MainActivity","onRestart called");
}
protected void onResume(){
super.onResume();
Log.i("MainActivity","onResume called");
}
protected void onPause(){
super.onPause();
Log.i("MainActivity","onPause called");
}
protected void onStop(){
super.onStop();
Log.i("MainActivity","onStop called");
}
protected void onDestroy(){
super.onDestroy();
Log.i("MainActivity","onDestroy called");
}
}
- Create a second activity (SecondActivity). Right click on Java folder.
Then select New->Activity->Empty Activity. In the SecondActivity class,
write the following code.
package com.example.dara. activitylifecycleexample;
import android.support.v7.app. AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Log.i("SecondActivity","onCreate called");
}
protected void onStart(){
super.onStart();
Log.i("SecondActivity","onStart called");
}
protected void onRestart(){
super.onRestart();
Log.i("SecondActivity","onRestart called");
}
protected void onResume(){
super.onResume();
Log.i("SecondActivity","onResume called");
}
protected void onPause(){
super.onPause();
Log.i("SecondActivity","onPause called");
}
protected void onStop(){
super.onStop();
Log.i("SecondActivity","onStop called");
}
protected void onDestroy(){
super.onDestroy();
Log.i("SecondActivity","onDestroy called");
}
}
Note: Every activity you define for your application must be declared in
your AndroidManifest.xml file and the main activity for your app must be
declared in the manifest with an <intent-filter> that includes the MAIN
action and LAUNCHER category.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android= "http://schemas.android.com/ apk/res/android"
package="com.example.dara. activitylifecycleexample ">
<application
android:allowBackup="true"
android:icon= "@mipmap/ic_launcher"
android:label= "@string/app_name"
android:supportsRtl="true"
android:theme= "@style/AppTheme">
<activity android:name= ".MainActivity">
<intent-filter>
<action android:name= "android.intent.action.MAIN" />
<category android:name= "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name= ".SecondActivity"></activity>
</application>
</manifest>
- Finally run your app, check log messages, and then press the
button (labeled Start Second Button). Try to press the back button
and check log messages.