It's Year 2022 and I'm here on this question. I just want to add to all the other answers here so other people who are here now have more examples. I found that adding configuration change to the manifest is the best way to handle the application resetting itself when orientation change. I like @Vuk answer the best for showing how to add it to the manifest.
I have this in my AndroidManifest.xml. I include screenLayout and screenSize for the older android version:
<activity
android:name=".MainActivity"
android:configChanges="screenLayout|orientation|screenSize"
android:exported="true">
</activity>
To handle the configuration change manually when you added to the manifest, add the following codes in the MainActivity.kt as an example of handling the screen orientation change. This just shows the toaster popup message, but you can handle it however you want.
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
//override stuff with orientation change, etc.
// 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()
}
}
Reference: https://developer.android.com/guide/topics/resources/runtime-changes