Skip to main content

How to handle orientation changes in fragments in Kotlin Android

How to handle orientation changes in fragments in Kotlin Android.

Here is a detailed step-by-step tutorial on how to handle orientation changes in fragments in Kotlin Android.

Step 1: Create a new Android project

Start by creating a new Android project in Android Studio. Choose an appropriate project name and set the minimum SDK version to support the devices you want to target.

Step 2: Create a new fragment

Next, create a new fragment class by right-clicking on the package where you want to create the fragment and selecting "New" -> "Fragment" -> "Fragment (Blank)". Give the fragment a name and click "Finish" to create the fragment.

Step 3: Update the fragment layout

Open the XML layout file associated with the fragment (e.g., fragment_my_fragment.xml) and make any necessary changes to the layout. For example, you might want to add some text views or buttons.

Step 4: Handle orientation changes in the fragment

To handle orientation changes in the fragment, you need to override the onCreateView() method and save any important data or state in the onSaveInstanceState() method.

In the onCreateView() method, inflate the fragment layout and return the root view. Here's an example:

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_my_fragment, container, false)
}

In the onSaveInstanceState() method, save any important data or state using the Bundle parameter. For example, you might want to save the text entered in an EditText view. Here's an example:

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
val editText = view?.findViewById<EditText>(R.id.editText)
val text = editText?.text?.toString()
outState.putString("text", text)
}

Step 5: Restore the fragment state after orientation changes

To restore the fragment state after an orientation change, you need to override the onActivityCreated() method and retrieve the saved data or state from the savedInstanceState parameter.

In the onActivityCreated() method, check if the savedInstanceState parameter is not null and retrieve the saved data or state. For example, you might want to restore the text entered in an EditText view. Here's an example:

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
if (savedInstanceState != null) {
val text = savedInstanceState.getString("text")
val editText = view?.findViewById<EditText>(R.id.editText)
editText?.setText(text)
}
}

Step 6: Add the fragment to the activity

To display the fragment in your activity, you need to add it to the activity's layout file or programmatically in the activity's onCreate() method.

If you choose to add the fragment programmatically, you can use the FragmentManager to begin a FragmentTransaction and add the fragment to a container view. Here's an example:

val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
val fragment = MyFragment()
fragmentTransaction.add(R.id.container, fragment)
fragmentTransaction.commit()

Make sure to replace MyFragment with the actual name of your fragment class, and R.id.container with the ID of the container view where you want to add the fragment.

Step 7: Test the app

Build and run your app on a device or emulator to test the fragment's behavior during orientation changes. Try rotating the device and verify that the fragment's state is preserved.

That's it! You have successfully handled orientation changes in fragments in Kotlin Android.