Activity Behavior Changes When the Screen Orientation Changes in Android

Introduction

 
This article explains the restart of an Activity device due to orientation changes in Android.
 
Some device configurations change while the application is running, such as when the device changes its orientation. Such a change restarts the activity by calling all its methods again. So when the device orientation changes, first the Activity will disappear for a millisecond when the onPause(), OnStop, and onDestroy() methods are called.
 
After a few milliseconds, the activity will be restarted when the onCreate(), onStart() and onResume() methods are called.
 
Activity LifeCycle
  1. onCreate(): This method is called when the Activity is created
  2. onStart(): This method is called when the Activity is 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 Activity is not visible to the user
  5. onStop(): This method is called when the Activity is no longer visible to the user
  6. onRestart(): This method is called when the Activity resumes after a stop
  7. onDestroy(): This method is called when the Activity is destroyed by the system
So let's see what happens from the start of an activity is created and the activity is destroyed.
 
Step 1
 
Create an Android project like this:
 
orie4
 
Step 3
 
Create a Java class file with the following.
 
In this, I have used all the lifecycle methods of an Activity that shows you how the methods are called when the activity creates, destroys and changes its orientation.
  1. package com.example.screenorientation;  
  2.    
  3. import android.os.Bundle;   
  4. import android.app.Activity;   
  5. import android.util.Log;   
  6. import android.view.Menu;   
  7.    
  8. public class MainActivity extends Activity {   
  9.    
  10.     @Override   
  11.   
  12. // onCreate method will be called when the activity is created  
  13.     protected void onCreate(Bundle savedInstanceState) {   
  14.         super.onCreate(savedInstanceState);   
  15.         setContentView(R.layout.activity_main);   
  16.         Log.d("cycle","onCreate wiil be invoke");   
  17.     }   
  18.     @Override   
  19.   
  20. // onStart() method will be called when the activity visible to the user  
  21.     protected void onStart() {   
  22.         super.onStart();   
  23.          Log.d("cycle","onStart wiil be invoked");   
  24.     }   
  25.     @Override   
  26.   
  27. onResume() method will be called when the activity starts intracting with the user  
  28.     protected void onResume() {   
  29.    
  30.         super.onResume();   
  31.          Log.d("cycle","onResume wiil be invoked");   
  32.     }   
  33.    
  34.  onPause() method will be called when the Activity is not  
  35.     @Override   
  36.   
  37. // This method is called when the is not visible to the use  
  38.     protected void onPause() {   
  39.    
  40.         super.onPause();   
  41.          Log.d("cycle","onPause wiil be invoked");   
  42.     }   
  43.     @Override   
  44.   
  45. // This methos is called when the Activity is no longer visible to the user  
  46.     protected void onStop() {   
  47.    
  48.         super.onStop();   
  49.          Log.d("cycle","onStop wiil be invoked");   
  50.     }   
  51.    
  52.        @Override   
  53.   
  54. // This method is called when the acvtivity is restarted  
  55.     protected void onRestart() {   
  56.    
  57.         super.onRestart();   
  58.          Log.d("cycle","onRestart wiil be invoked");   
  59.     }      
  60.     @Override   
  61.   
  62. This method is called when the activity is destroyed  
  63.    
  64.     protected void onDestroy() {   
  65.    
  66.         super.onDestroy();   
  67.          Log.d("cycle","onDestroy wiil be invoked");   
  68.     }   
  69. }   
So when you start the Activity the onCreate(), onStart() and onReume() methods will be called simultaneously.
 
orie1
 
After clicking on the back button, first the onPause() method is called then after some time the onStop() and OnDestroy() methods will be called simultaneously.
 
ori2
 
Now see what happens when the screen orientation changes.
 
First onPause(), onStop() and onDestroy() will be called simultaneously and after some time when the activity restarts the onCreate(), onStart() and onReume() methods will be called simultaneously.
 
orie3
 
So when you change the orientation of a screen the Activity will be restarted.


Similar Articles