Add this to your manifest:
android:configChanges="orientation|keyboardHidden
Then in your activity add this somewhere:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
{
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_hls_port);
}
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
{
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_hls_port );
}
}
I'm not quite sure why the error is being caused, though when I tested it does not throw the exception--I wouldn't call it a fix but more of a workaround.
The line in the manifest file prevents the activity from being restarted when the device is rotated. Then when you override onConfigurationChanged()
, a method that's called when the device is rotated, and since your're overriding it you have to specify the correct layout for portrait/orientation.
Note that onConfigurationChanged()
may not just be called when the device is rotated--there are many others times this may be called, you can see the complete list at the android docs. As such, it may not be good to perform whatever is done in that method when, say, the locale (language) has changed. You can read more about this here.
Also note that the without android:configChanges
in your manifest the onConfigurationChanged()
method does nothing.
Best of luck!