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. import android.support.v7.app.ActionBarActivity;
  3. import android.os.Bundle;
  4. import android.util.Log;
  5. import android.view.Menu;
  6. import android.view.MenuItem;
  7. public class MainActivity extends ActionBarActivity {
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. Log.d("ChangeStateInfo","onCreate");
  13. }
  14. @Override
  15. public void onStart(){
  16. Log.d("ChangeStateInfo", "onStart");
  17. super.onStart();
  18. }
  19. @Override
  20. public void onResume(){
  21. Log.d("ChangeStateInfo","onResume");
  22. super.onResume();
  23. }
  24. @Override
  25. public void onPause(){
  26. Log.d("ChangeStateInfo","onResume");
  27. super.onPause();
  28. }
  29. @Override
  30. public void onStop(){
  31. Log.d("ChangeStateInfo","onStop");
  32. super.onStop();
  33. }
  34. @Override
  35. public void onDestroy(){
  36. Log.d("ChangeStateInfo","onDestroy");
  37. super.onDestroy();
  38. }
  39. @Override
  40. public void onRestart(){
  41. Log.d("ChangeStateInfo","onRestart");
  42. super.onRestart();
  43. }
  44. @Override
  45. public boolean onCreateOptionsMenu(Menu menu) {
  46. // Inflate the menu; this adds items to the action bar if it is present.
  47. getMenuInflater().inflate(R.menu.menu_main, menu);
  48. return true;
  49. }
  50. @Override
  51. public boolean onOptionsItemSelected(MenuItem item) {
  52. // Handle action bar item clicks here. The action bar will
  53. // automatically handle clicks on the Home/Up button, so long
  54. // as you specify a parent activity in AndroidManifest.xml.
  55. int id = item.getItemId();
  56. //noinspection SimplifiableIfStatement
  57. if (id == R.id.action_settings) {
  58. return true;
  59. }
  60. return super.onOptionsItemSelected(item);
  61. }
  62. }
    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.