I have all activities in portrait mode except the one that I use to play a video that is always landscape. I found that on Android 8.1 every time I open the video activity and close it the previous activity go to landscape even it's set to "portrait" on the manifest.
- Sometimes goes to portrait then to landscape and stay on landscape.
- Sometimes goes to portrait then to landscape and finally portrait again.
This is only happening when a go back from a activity that it's landscape.
There is anyone who is experiencing this?
Thanks.
EDIT
I report the bug on Google: https://issuetracker.google.com/issues/69168442
EDIT 2
It seems fixed on Android 9
I had such problem and been trying all of above use cases. Most of them work, but there is one case you should know:
The final reason was the using of fragments inside an activity content layout in case of Android 8. For example: The activity launches in Landscape mode, but the fragment shows you the Portrait layout.
Try to avoid fragments.
To fix this issue:
When you will back to Activity A from Activity B and if you know the screen's orientation of the Activity A, so set the screen orientation into the ondestroy of Activity B.
The next workaround may help you to solve the issue.
You must extend all your activities using the one in the code. It takes care of setting and restoring the correct orientations whenever the onPause / onResume methods are called.
The workaround will work for any type of orientation defined in the manifest activity tags.
For my own purposes I extend this class from ComponentActivity, so you may want to change this to extend from Activity, ActivityCompat, or whatever type of activity you are using in your code.
Using
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
in previous Activity resolved my issue.from Narmi's answer:
you have to detect if activity is destroying from configuration change, so add field
isConfigurationChanged = false
, then ononSaveInstanceState
method turn it totrue
and ononDestroy
method add this:If u have DialogTheme like theme=Theme.AppCompat.Light.Dialog in your manifesto, remove the set orientation tag for that Dialog activity from manifesto , It will take the Orientation from the previous activity and put setorientation tag for remaing activites
and for below Versions place this on oncreate
and set theme for application Theme.AppCompat.Light.DarkActionBar no need to add theme to activities