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 a
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.
public abstract class AbsBaseActivity extends ComponentActivity
{
private int currentActivityOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
private int parentActivityOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
@CallSuper
@Override
protected void onCreate(@Nullable final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.cacheOrientations();
}
private void cacheOrientations()
{
if (this.currentActivityOrientation == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
{
final Intent parentIntent = this.getParentActivityIntent();
if (parentIntent != null)
{
final ComponentName parentComponentName = parentIntent.getComponent();
if (parentComponentName != null)
{
this.currentActivityOrientation = this.getConfiguredOrientation(this.getComponentName());
this.parentActivityOrientation = this.getConfiguredOrientation(parentComponentName);
}
}
}
}
private int getConfiguredOrientation(@NonNull final ComponentName source)
{
try
{
final PackageManager packageManager = this.getPackageManager();
final ActivityInfo activityInfo = packageManager.getActivityInfo(source, 0);
return activityInfo.screenOrientation;
}
catch (PackageManager.NameNotFoundException e)
{
e.printStackTrace();
}
return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
}
@CallSuper
@Override
protected void onPause()
{
if (this.parentActivityOrientation != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
{
this.setRequestedOrientation(this.parentActivityOrientation);
}
super.onPause();
}
@CallSuper
@Override
protected void onResume()
{
super.onResume();
if (this.currentActivityOrientation != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
{
this.setRequestedOrientation(this.currentActivityOrientation);
}
}
}
Using intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
in previous Activity resolved my issue.
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.
@Override
protected void onDestroy() {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onDestroy();
}
This fixed the issue.
Override the onBackPressed() method of Landscape activity and set orientation to Portrait.
@Override
public void onBackPressed() {
super.onBackPressed();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
on the onCreate method of each activity insert this line
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
for the landscape and
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
for the portrait ones
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
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
and set theme for application Theme.AppCompat.Light.DarkActionBar no need to add theme to activities