In this tutorial you will learn about the Android Activity Lifecycle and its application with practical example.
Activity is the single screen like java frame or window, The Activity class provides sequence of callback methods: onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), onDestroy().Each Callback Method invoke when Activity enters in new state.
Like main() method in C/C++ or Java Activity need onCreate() method for initiating its programming. As we discussed in above point that Activity Provides 7 callback methods but it doesn’t mean that you need to implement all callback methods.
Lets clarify these callback methods by below diagram:
1.onCreate() : This callback method called when the activity is first created.
- onStart() : This method called when the activity becomes visible to user.
- onResume() : This method called When the user start interaction with App.
- onPause() : This method called when current activity is not visible to user and user is unable to send input and can not execute any code. In this method Current Activity will be on Pause condition and Previous Activity will be in OnResume() condition.
- onStop() : This callback method called when Activity is no longer visible to the user.
- onDestroy() : This method called before the activity destroyed by System. This method invoked if someone’s calling finish(), else system destroy temporarily the process to save space.
- onRestart() : This callback method called when the activity restart after stopping.
Lets Understand Android Activity Lifecycle with the help of below Example. Follow the following steps for modifying the “Hello World” App. I hope you remember all the steps which we have done in “Hello World” Android Application, Here we have to do some modification for understanding Lifecycle of Activity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
package com.example.myapplication; import android.os.Bundle; import android.app.Activity; import android.util.Log; public class MainActivity extends AppCompatActivity { String str_message = "Android : "; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(str_message, "The onCreate() event"); } /** Called when the activity is about to become visible. */ @Override protected void onStart() { super.onStart(); Log.d(str_message, "The onStart() event"); } /** Called when the activity has become visible. */ @Override protected void onResume() { super.onResume(); Log.d(str_message, "The onResume() event"); } /** Called when another activity is taking focus. */ @Override protected void onPause() { super.onPause(); Log.d(str_message, "The onPause() event"); } /** Called when the activity is no longer visible. */ @Override protected void onStop() { super.onStop(); Log.d(str_message, "The onStop() event"); } /** Called when activity restart after stopping ,. */ @Override protected void onStop() { super.onStop(); Log.d(str_message, "The onStop() event"); } /** Called just before the activity is destroyed. */ @Override public void onDestroy() { super.onDestroy(); Log.d(str_message, "The onDestroy() event"); } } |
Let’s run this modified code on AVD, We are done with coding. Follow the same steps as you have done in “Hello World” Application.
After Running your Application you need to check your LogCat. If you are unable to find logcat screen then just click alt+6 and check LogCat with “Android:” Message.