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

onCreate():
This method is called when the Activity is created

onStart(): This method is called when the Activity is visible to the user
 
onResume()
: This method is called when the Activity starts interacting with the user
 
onPause():
This method is called when the Activity is not visible to the user

onStop():
This method is called when the Activity is no longer visible to the user

onRestart():
This method is called when the Activity resumes after a stop

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.

package com.example.screenorientation;

 

import android.os.Bundle; 

import android.app.Activity; 

import android.util.Log; 

import android.view.Menu; 

 

public class MainActivity extends Activity { 

 

    @Override 

// onCreate method will be called when the activity is created

    protected void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.activity_main); 

        Log.d("cycle","onCreate wiil be invoke"); 

    } 

    @Override 

// onStart() method will be called when the activity visible to the user

    protected void onStart() { 

        super.onStart(); 

         Log.d("cycle","onStart wiil be invoked"); 

    } 

    @Override 

onResume() method will be called when the activity starts intracting with the user

    protected void onResume() { 

 

        super.onResume(); 

         Log.d("cycle","onResume wiil be invoked"); 

    } 

 

 onPause() method will be called when the Activity is not

    @Override 

// This method is called when the is not visible to the use

    protected void onPause() { 

 

        super.onPause(); 

         Log.d("cycle","onPause wiil be invoked"); 

    } 

    @Override 

// This methos is called when the Activity is no longer visible to the user

    protected void onStop() { 

 

        super.onStop(); 

         Log.d("cycle","onStop wiil be invoked"); 

    } 

 

       @Override 

// This method is called when the acvtivity is restarted

    protected void onRestart() { 

 

        super.onRestart(); 

         Log.d("cycle","onRestart wiil be invoked"); 

    }    

    @Override 

This method is called when the activity is destroyed
 

    protected void onDestroy() { 

 

        super.onDestroy(); 

         Log.d("cycle","onDestroy wiil be invoked"); 

    } 

 

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.

Up Next
    Ebook Download
    View all
    Learn
    View all