Skip to main content

How to handle orientation changes in Kotlin Android

How to handle orientation changes in Kotlin Android.

Here's a detailed step-by-step tutorial on how to handle orientation changes in Kotlin Android:

  1. Understanding Orientation Changes:

    • In Android, when the device is rotated, the current Activity is destroyed and recreated to accommodate the new orientation.
    • This can lead to issues like loss of data and UI inconsistencies if not handled properly.
  2. Handling Orientation Changes:

    • To handle orientation changes, you need to save and restore the state of your Activity.
    • There are multiple approaches to achieve this. Let's explore a few of them.
  3. Using onSaveInstanceState() and onRestoreInstanceState():

    • Override the onSaveInstanceState() method in your Activity to save the state of any important data or variables.

    • This method is called before the Activity is destroyed, so it's a good place to save any necessary information.

    • Example code:

      override fun onSaveInstanceState(outState: Bundle) {
      outState.putString("key", value)
      super.onSaveInstanceState(outState)
      }
    • Override the onRestoreInstanceState() method to restore the saved state when the Activity is recreated.

    • This method is called after the Activity is recreated, so you can restore any saved data here.

    • Example code:

      override fun onRestoreInstanceState(savedInstanceState: Bundle) {
      super.onRestoreInstanceState(savedInstanceState)
      value = savedInstanceState.getString("key")
      }
  4. Using ViewModel:

    • Android Architecture Components provide a ViewModel class that survives configuration changes like orientation changes.

    • Create a ViewModel class that holds the data you want to retain across orientation changes.

    • Example code:

      class MyViewModel : ViewModel() {
      var value: String = ""
      }
    • In your Activity, create an instance of the ViewModel and access the data from it.

    • Example code:

      class MyActivity : AppCompatActivity() {
      private val viewModel: MyViewModel by viewModels()

      override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)

      viewModel.value = "Hello, World!"
      }
      }
  5. Using ViewModel with LiveData:

    • LiveData is an observable data holder class that can be used with ViewModel to handle orientation changes.

    • Create a LiveData object in your ViewModel to hold the data.

    • Example code:

      class MyViewModel : ViewModel() {
      private val _value = MutableLiveData<String>()
      val value: LiveData<String> get() = _value

      fun setValue(newValue: String) {
      _value.value = newValue
      }
      }
    • Observe the LiveData in your Activity and update the UI accordingly.

    • Example code:

      class MyActivity : AppCompatActivity() {
      private val viewModel: MyViewModel by viewModels()

      override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)

      viewModel.value.observe(this) { value ->
      textView.text = value
      }

      viewModel.setValue("Hello, World!")
      }
      }
  6. Using Configuration Changes in Manifest:

    • You can also prevent your Activity from being destroyed and recreated on orientation changes by handling it in the manifest file.

    • Add the android:configChanges attribute to your Activity declaration in the manifest file.

    • Example code:

      <activity
      android:name=".MyActivity"
      android:configChanges="orientation|screenSize">
      </activity>
    • Override the onConfigurationChanged() method in your Activity to handle the orientation change manually.

    • Example code:

      override fun onConfigurationChanged(newConfig: Configuration) {
      super.onConfigurationChanged(newConfig)
      // Handle orientation change manually
      }

That's it! You've learned multiple ways to handle orientation changes in Kotlin Android. Choose the approach that suits your needs and implement it in your app.