Activity Behavior When Screen Orientation Changes In Android Application

Overview
 
Here, some useful links are given to start programming in Android Applications-.
  1. Android Programming - Day One
  2. Android Programming - Day Two
  3. How To Display A Dialog Windows In Android
  4. How To Display A Progress Dialog Window In Android
  5. Intent In Android
  6. Return Data Using Intent Object In Android Applications
  7. Passing Data Using An Intent Object In Android Applications
  8. Fragments In Android Applications
  9. Adding Fragments Dynamically In Android Application
  10. Fragment Life Cycle In Android Application
  11. Interaction Between Two Fragments
  12. Calling In-Built Functions in Android Application
  13. Intent Object and Intent Filters in Android Application
  14. Displaying Notifications in Android Applications
  15. User Interface in Android Applications
  16. Orientation, Anchoring, Resizing And Repositioning Of Views In Android Application

Introduction

 
In this article, you will learn about an activity behavior of the views, when screen orientation changes to either portrait or a landscape. In the article Orientation, Anchoring, Resizing And Repositioning Of Views In Android Application, I talked about the different type of screen orientation. The activity life cycle, I have covered in this article, Android Programming - Day Two . Now, it’s very important to know about the activity’s state, when the device changes an orientation.
 
Implementation
 
Create a new project by selecting the File->New->New Project.
 
New Project
 
Add 2 EditText element in the activity_main.xml file. Change the layout as LinearLayout.
  1. <LinearLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"    
  4. android:layout_width="fill_parent"    
  5. android:layout_height="fill_parent"    
  6. android:orientation="vertical"    
  7. tools:context=".MainActivity">  
  8.     <EditText    
  9. android:layout_width="fill_parent"    
  10. android:layout_height="wrap_content"    
  11. android:id="@+id/txt1"    
  12. />  
  13.     <EditText    
  14. android:layout_width="fill_parent"    
  15. android:layout_height="wrap_content"    
  16. android:id="@+id/txt2"    
  17. />  
  18. </LinearLayout>    
Add the code, given below, in the MainActivity.java file,
  1. package com.example.administrator.orientationapp;    
  2.          
  3. import android.support.v7.app.ActionBarActivity;    
  4. import android.os.Bundle;    
  5. import android.util.Log;    
  6. import android.view.Menu;    
  7. import android.view.MenuItem;    
  8.          
  9. public class MainActivity extends ActionBarActivity {    
  10.         
  11.    @Override    
  12.     protected void onCreate(Bundle savedInstanceState) {    
  13.         super.onCreate(savedInstanceState);    
  14.         setContentView(R.layout.activity_main);    
  15.         Log.d("ChangeStateInfo","onCreate");    
  16.     }    
  17.     @Override    
  18.     public void onStart(){    
  19.         Log.d("ChangeStateInfo""onStart");    
  20.         super.onStart();    
  21.     }    
  22.     @Override    
  23.     public void onResume(){    
  24.         Log.d("ChangeStateInfo","onResume");    
  25.         super.onResume();    
  26.     }    
  27.     @Override    
  28.     public void onPause(){    
  29.        Log.d("ChangeStateInfo","onResume");    
  30.         super.onPause();    
  31.     }    
  32.     @Override    
  33.     public void onStop(){    
  34.         Log.d("ChangeStateInfo","onStop");    
  35.         super.onStop();    
  36.     }    
  37.     @Override    
  38.     public void onDestroy(){    
  39.         Log.d("ChangeStateInfo","onDestroy");    
  40.         super.onDestroy();    
  41.     }    
  42.     @Override    
  43.     public void onRestart(){    
  44.         Log.d("ChangeStateInfo","onRestart");    
  45.         super.onRestart();    
  46.     }    
  47.          
  48.     @Override    
  49.     public boolean onCreateOptionsMenu(Menu menu) {    
  50.         // Inflate the menu; this adds items to the action bar if it is present.    
  51.         getMenuInflater().inflate(R.menu.menu_main, menu);    
  52.         return true;    
  53.     }    
  54.          
  55.     @Override    
  56.     public boolean onOptionsItemSelected(MenuItem item) {    
  57.        // Handle action bar item clicks here. The action bar will    
  58.        // automatically handle clicks on the Home/Up button, so long    
  59.        // as you specify a parent activity in AndroidManifest.xml.    
  60.         int id = item.getItemId();    
  61.          
  62.         //noinspection SimplifiableIfStatement    
  63.         if (id == R.id.action_settings) {    
  64.             return true;    
  65.         }    
  66.          
  67.         return super.onOptionsItemSelected(item);    
  68.     }    
  69. }   
    Explanation
     
    Run the Application by clicking F11 on either Android Emulator or device. When the views are in portrait mode, it looks like the image, given below,
     
    mode
     
    The state sequence is in the following order when the views are in the portrait state initially,
     
    state
     
    As soon as the state is changed from portrait to landscape, it looks like the image, given below,
     
    landscape
     
    The state sequence is in the following order,
     
    landscape
     
    From the above output, we see that the activity is destroyed or resumed when the device changes the orientation from landscape to portrait or vice versa.
    Afterward, it again recreates.
     
    state
     
    Thus, it is very important to understand the behavior because you need to ensure that you take the necessary steps to preserve the state of your activity before it changes orientation. Suppose, your activity may have the variables that contain the values, required for some calculations in the activity. For any activity, you will have to save whatever state you need to save in the onResume() method, which is fired every time the activity changes an orientation.
     
    There is another important behavior to understand. Is it only View which has some Id defined in an activity? They will have their state persisted when the activity they are contained in is destroyed or resumed. Suppose, the user may change an orientation while entering some text into an EditText View. When this happens, any text inside the EditText View will persist; and restored automatically when the activity is recreated.
     

    Conclusion

     
    In the article, you learned the activity behavior when the screen orientation changes. In the next article, I will explain about persisting the state information, when changes in configuration occur.