Main navigation

Activity Lifecycle and Debugging of an Android App.

adsense

Pre-requisites:

Android Studio installed on your PC (Unix or Windows). You can learn how to install it here .

Activity Lifecycle:

Lifecycle can be easily described through following block diagram:

App_debug_1

  1. onCreate(): This is called initially when activity is created. Here we define all normal views and methods for binding data. This is always followed by onStart as shown in block diagram too.
  2. onStart(): After onCreate(), Activity is created but still invisible to user. To make it visible onStart() is called.
  3. onResume() : An App doesn’t have any meaning if it is just visible to user. This callback is responsible for all interaction between user and App. Now your App is running fine.
  4. onPause() : This method is called when a previous activity start resuming.
  5. onStop() : This method is called when another activity is resumed. Due to this current running Activity become invisible to user. Another activity could be a new Activity or an existing one being brought in front of this one.
  6. onDestroy() : This method is called when Activity is finally destroyed and no longer visible to user. This could be due to two reasons: (a) App is finished successfully (b) OS stopped this App to save some space.

Every Activity instance has a lifecycle and during this Activity Lifecycle it goes through above stages. Now to practically see significance of these methods, we will include debug prints at each stage of Activity Lifecycle.

Function Public static int d(String tag, String message) shall be used for debug prints. Here tag is the source of message and message is the output to print.

Creating new project

Please follow following steps:

  1. Open Android Studio and make a new project with name My Application and company domain application.example.com (I used my company domain i.e androidtutorialpoint.com. Similarly you can use yours).
  2. Click Next and choose android version Lollipop. Again Click Next and Choose Blank Activity.
  3. Leave all things remaining same and Click Finish.

Now you can see MainActivity.java, content_main.xml and strings.xml. If you are not able to see them then click on the left window where these files can easily be located as shown in following figure:

App_debug_2

MainActivity.java:

Declare variable string TAG = “MainActivity” in class MainActivity

private static final String TAG = "MainActivity";

Add following code in the onCreate() method of MainActivity.java :

Log.d(TAG, "onCreate is called");

Finally override predefined methods onStart(), onPause() etc, in MainActivity.java :

    @Override 
    public void onStart() { 
        super.onStart(); 
        Log.d(TAG, "onStart() called"); 
    } 
    @Override 
    public void onPause() { 
        super.onPause(); 
        Log.d(TAG, "onPause() called"); 
    } 
    @Override 
    public void onResume() { 
        super.onResume(); 
        Log.d(TAG, "onResume() called"); 
    } 
    @Override 
    public void onStop() { 
        super.onStop(); 
        Log.d(TAG, "onStop() called"); 
    } 
    @Override 
    public void onDestroy() { 
        super.onDestroy(); 
        Log.d(TAG, "onDestroy() called"); 
    }
	

Note: @Override asks compiler to check if the method is already defined which it trying to override. If it doesn’t exist then compiler will throw error.

To see output prints at run time we will use LogCat. Go to the Bottom Right of your android studio screen and choose Edit Filter Configuration . Here fill Filter Name as MainActivity and similarly Log Tag as the name of TAG (MainActivity) you choose above:

App_debug_3

Click Ok and run code on any Android Device. You will see following debug prints:

App_debug_4

Now we can play with it. We will click Home, Recent and Back button of our android device to see how they impact Activity Lifecycle.

App_debug_7

Fig: Home, Back and Recent buttons

Press Home button and check log print. It will show onPause() –> onStop() as shown in following fig.:

App_debug_5

Press Back button. It implies you don’t want to see this App anymore and hence it will destroy Activity. It will call onPause() –> onStop() –> onDestroy() as shown:

App_debug_6

In this post, we saw how to add debug print in an android App and what is Activity Lifecycle of an android App.



What’s Next:

You can see our next tutorial of How to make a basic App. Here You will be able to make an App from scratch without any external help.

Please comment if you have any doubts or suggestions. Don’t forget to subscribe. Also do Like our Facebook Page or Add us on Twitter. Have a nice day.

To download full code of this post please click here:


Android Tutorial Point Download Now


Reader Interactions

Comments

Leave a Reply

Advertisment ad adsense adlogger