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. 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. @Override
  8. // onCreate method will be called when the activity is created
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. Log.d("cycle","onCreate wiil be invoke");
  13. }
  14. @Override
  15. // onStart() method will be called when the activity visible to the user
  16. protected void onStart() {
  17. super.onStart();
  18. Log.d("cycle","onStart wiil be invoked");
  19. }
  20. @Override
  21. onResume() method will be called when the activity starts intracting with the user
  22. protected void onResume() {
  23. super.onResume();
  24. Log.d("cycle","onResume wiil be invoked");
  25. }
  26. onPause() method will be called when the Activity is not
  27. @Override
  28. // This method is called when the is not visible to the use
  29. protected void onPause() {
  30. super.onPause();
  31. Log.d("cycle","onPause wiil be invoked");
  32. }
  33. @Override
  34. // This methos is called when the Activity is no longer visible to the user
  35. protected void onStop() {
  36. super.onStop();
  37. Log.d("cycle","onStop wiil be invoked");
  38. }
  39. @Override
  40. // This method is called when the acvtivity is restarted
  41. protected void onRestart() {
  42. super.onRestart();
  43. Log.d("cycle","onRestart wiil be invoked");
  44. }
  45. @Override
  46. This method is called when the activity is destroyed
  47. protected void onDestroy() {
  48. super.onDestroy();
  49. Log.d("cycle","onDestroy wiil be invoked");
  50. }
  51. }
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.