Android Solutions – Configuration Change

1
User Management System
Learn Kotlin, Retrofit, MVVM and MySQL using this all-in-one app. It is designed to be beginner friendly.

In this piece we look at android solutions related to configuration changes in the android device.

Here's a simple note about handling configuration changes according to android official documentation:

Some device configurations can change during runtime (such as screen orientation, keyboard availability, and when the user enables multi-window mode). When such a change occurs, Android restarts the running Activity ( onDestroy() is called, followed by onCreate()). The restart behavior is designed to help your application adapt to new configurations by automatically reloading your application with alternative resources that match the new device configuration.

To properly handle a restart, it is important that your activity restores its previous state. You can use a combination of onSaveInstanceState(), ViewModel objects, and persistent storage to save and restore the UI state of your activity across configuration changes. Read more about it here.

How do you listen to configuration changes in android?

Step 1 - Specify configChanges property in android manifest

Like this:

<activity android:name=".MyActivity"
          android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
          android:label="@string/app_name">

The code declares an activity that handles both screen orientation changes and keyboard availability change.

  • The "orientation" value prevents restarts when the screen orientation changes.
  • The "screenSize" value also prevents restarts when orientation changes, but only for Android 3.2 (API level 13) and above.
  • The "screenLayout" value is necessary to detect changes that can be triggered by devices such as foldable phones and convertible Chromebooks.
  • The "keyboardHidden" value prevents restarts when the keyboard availability changes.

Step 2: Override onConfigurationChanged method

Like this:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    int newOrientation = newConfig.orientation;

    if (newOrientation == Configuration.ORIENTATION_LANDSCAPE) {
      // show("Landscape");
    } else if (newOrientation == Configuration.ORIENTATION_PORTRAIT){
    // show("Portrait");
    }  
}

Or you can use a switch statement:

int orientation=newConfig.orientation;

switch(orientation) {

case Configuration.ORIENTATION_LANDSCAPE:

//to do something
 break;

case Configuration.ORIENTATION_PORTRAIT:

//to do something
 break;

}

In Kotlin:

override fun onConfigurationChanged(newConfig: Configuration) {
    super.onConfigurationChanged(newConfig)

    // Checks the orientation of the screen
    if (newConfig.orientation === Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show()
    } else if (newConfig.orientation === Configuration.ORIENTATION_PORTRAIT) {
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show()
    }
}

Note that after before writing your implementation code you need to add super.onConfigurationChanged(newConfig); or else an exception may be raised.

Observations

The onConfigurationCalled() may not be called if you

1.Set layout to landscape in XML

android:screenOrientation="landscape"

2. Invoke setRequestedOrientation manually

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

3. You have both "android:screenOrientation" and "android:configChanges" specified.

Learn Android Retrofit using our course

Android MySQL Retrofit2 Multipart CRUD,Search,Pagination rating

When I was a 2nd year Software Engineering student, I buillt a now defunct online tool called Camposha(from Campus Share) using my then favorite language C#(ASP.NET) to compete OLX in my country(Kenya). The idea was to target campus students in Kenya. I got a few hundred signups but competing OLX proved too daunting. I decided to focus on my studies, learning other languages like Java,Python,Kotlin etc while meanwhile publishing tutorials at my YouTube Channel ProgrammingWizards TV which led to this site(camposha.info). Say hello or post me a suggestion: oclemmi@gmail.com . Follow me below; Github , and on my channel: ProgrammingWizards TV

We will be happy to hear your thoughts

Leave a reply

seventy seven + = seventy nine

Reset Password
Compare items
  • Total (0)
Compare
0
Shopping cart