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);
}
}
}