Learning Android Application Development - Day 3

Introduction

 
Hope you all are doing well, first of all, thank you so much for reading my previous blogs:
Today we will learn about the Activity in Android, how to switch from one activity to another activity, what the activity life cycle is, how to create activity in android and how to destroy an activity.
 

What is Activity?

 
Activity in the android is a service provided by the android to handle different pages, basically, activity is the portion of the application that shows on the front end, and the UI shows the Activity so that you can place all your Widgets on it. An activity is a single screen in android. It is like a window or frame of Java.
 

What is Activity Life Cycle?

 
Every activity has its own life cycle that whenever any activity is created it has to be destroyed as well after completing the works. Every new activity should be registered in the menifest.xml file as I have explained in the first part of article series: Learning Android Application Development - Day 1.
 
The Activity life cycle has 7 stages or methods and these are mentioned below -
  1. onCreate ( )- This method is called in onCreate() method when an activity starts.
  2. onStart ( )- This method is called when it becomes visible to the user.
  3. onResume( )- This method is called when the activity starts interacting with the user.
  4. onPause( )- This method is called when the user wants to switch to another activity.
  5. onStop( )- This method is called when activity no longer visible to the user.
  6. onRestart( )- This method is called when the user wants to restart the activity.
  7. onDestroy( )- This method is called before the activity is destroyed.

Activity Life Cycle

 
Activity Life Cycle
 

How to create Activity in MainActivity.java?

 
MainActivity.java
  1. package com.example.activitylifecycle;    
  2. import android.os.Bundle;    
  3. import android.app.Activity;    
  4. import android.util.Log;    
  5. import android.view.Menu;    
  6. public class MainActivity extends Activity    
  7. {@    
  8.     Override    
  9.     protected void onCreate(Bundle savedInstanceState)    
  10.     {    
  11.         super.onCreate(savedInstanceState);    
  12.         setContentView(R.layout.activity_main);    
  13.         Log.d("my_lifecycle""onCreate Method is invoked");    
  14.     }@    
  15.     Override    
  16.     protected void onStart()    
  17.     {    
  18.         super.onStart();    
  19.         Log.d("my_lifecycle ""onStart Method is invoked");    
  20.     }@    
  21.     Override    
  22.     protected void onResume()    
  23.     {    
  24.         super.onResume();    
  25.         Log.d("my_lifecycle ""onResume Method is invoked");    
  26.     }@    
  27.     Override    
  28.     protected void onPause()    
  29.     {    
  30.         super.onPause();    
  31.         Log.d("my_lifecycle ""onPause Method is invoked");    
  32.     }@    
  33.     Override    
  34.     protected void onStop()    
  35.     {    
  36.         super.onStop();    
  37.         Log.d("my_lifecycle ""onStop Method is invoked");    
  38.     }@    
  39.     Override    
  40.     protected void onRestart()    
  41.     {    
  42.         super.onRestart();    
  43.         Log.d("my_lifecycle ""onRestart Method is invoked");    
  44.     }@    
  45.     Override    
  46.     protected void onDestroy()    
  47.     {    
  48.         super.onDestroy();    
  49.         Log.d("my_lifecycle ""onDestroy Method is invoked");    
  50.     }    
  51. }   
I am using here Log Tag so that you can see in your logcat that how and when the activity is created, stopped and destroyed.
 
A small example on button click to go to another activity
 
MainActivty.java
  1. package com.example.lucifer.myapplication;    
  2.     
  3. import android.app.Activity;    
  4. import android.content.Intent;    
  5. import android.os.Bundle;    
  6. import android.view.View;    
  7. import android.widget.Button;    
  8.     
  9.     
  10. public class MainActivity extends Activity {    
  11.     
  12.     @Override    
  13.     protected void onCreate(Bundle savedInstanceState) {    
  14.         super.onCreate(savedInstanceState);    
  15.         setContentView(R.layout.activity_main);    
  16.         Button b = (Button) findViewById(R.id.second_activity_button);    
  17.         b.setOnClickListener(new View.OnClickListener() {    
  18.       @Override    
  19.             public void onClick(View v) {    
  20.     
  21.                 Intent intent = new Intent(MainActivity.this,SecondActivity.class);    
  22.                 startActivity(intent);    
  23.             }    
  24.         });    
  25.     }    
  26. }   
    activity_main.xml
    1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      
    2.     android:id="@+id/layout_login"      
    3.     android:layout_width="fill_parent"      
    4.     android:layout_height="wrap_content"      
    5.     android:orientation="vertical"      
    6.     android:layout_gravity="top">      
    7.       
    8.     <TextView      
    9.         android:id="@+id/heading_main"      
    10.         android:layout_height="wrap_content"      
    11.         android:layout_width="wrap_content"      
    12.         android:text="MainActivity"      
    13.         android:textSize="20sp"      
    14.         android:textStyle="bold"      
    15.         android:layout_gravity="center_horizontal"      
    16.         android:layout_marginTop="10dp"      
    17.         />      
    18.       
    19.     <LinearLayout      
    20.         android:layout_width="match_parent"      
    21.         android:layout_height="wrap_content"      
    22.         android:layout_marginTop="60dp"      
    23.         android:orientation="horizontal"      
    24.         android:layout_gravity="center_horizontal" >      
    25.       
    26.         <Button      
    27.             android:id="@+id/second_activity_button"      
    28.             android:layout_gravity="center"      
    29.             android:layout_width="wrap_content"      
    30.             android:layout_height="wrap_content"      
    31.             android:layout_marginLeft="140dp"      
    32.             android:text="Go to Second" />      
    33.     </LinearLayout>      
    34.       
    35. </LinearLayout>     
      SecondActivty.java
      1. package com.example.lucifer.myapplication;    
      2.     
      3. import android.app.Activity;    
      4. import android.os.Bundle;    
      5.     
      6. public class SecondActivity extends Activity {    
      7.     
      8.     @Override    
      9.     protected void onCreate(Bundle savedInstanceState) {    
      10.         super.onCreate(savedInstanceState);    
      11.         setContentView(R.layout.second_activtiy);    
      12.     }    
      13. }   
        second_activtiy.xml
        1. <?xml version="1.0" encoding="utf-8"?>    
        2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
        3.     android:layout_width="match_parent"    
        4.     android:layout_height="match_parent"    
        5.     android:orientation="vertical"    
        6.     android:gravity="center">    
        7.     <TextView    
        8.         android:layout_width="wrap_content"    
        9.         android:layout_height="wrap_content"    
        10.         android:id="@+id/my_text"    
        11.         android:gravity="center"    
        12.         android:text="Second Activity"    
        13.         android:textSize="40dp"/>    
        14.     
        15. </LinearLayout>   
        AndroidManifest.xml
        1. <?xml version="1.0" encoding="utf-8"?>    
        2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"    
        3.     package="com.example.lucifer.myapplication">    
        4.     
        5.     <application    
        6.         android:allowBackup="true"    
        7.         android:icon="@mipmap/ic_launcher"    
        8.         android:label="@string/app_name"    
        9.         android:supportsRtl="true"    
        10.         android:theme="@style/AppTheme">    
        11.         <activity android:name="com.example.lucifer.myapplication.MainActivity">    
        12.             <intent-filter>    
        13.                 <action android:name="android.intent.action.MAIN" />    
        14.     
        15.                 <category android:name="android.intent.category.LAUNCHER" />    
        16.             </intent-filter>    
        17.         </activity>    
        18.         <activity android:name=".SecondActivity">    
        19.     
        20.         </activity>    
        21.     </application>    
        22.     
        23. </manifest>   
          Result
           
          Result
           
          Thank you so much for reading.
           

          Summary

           
          I hope you all are enjoying learning android. Next time I will be back with something new about Android.
           
          Keep learning, Happy coding!