0

I am developing an App on 8.1.0 Android Phone. I have four Activity. The first is AskPermissionActivity , it will close and change to the Second Activity (DeviceListActivity) after check the permission.

When I select the item in Second Activity (DeviceListActivity) , it will change to the Third Activity (MainInfoActivity).

And the screen Orientation of Third Activity (MainInfoActivity) is landscape , and the other is portrait. like the following code in AndroidManifest:

<!-- To access Google+ APIs: -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- BT -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<!-- GPS -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- EXTERNAL STORAGE -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher_aitrix1"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".AskPermissionActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".DeviceListActivity"
        android:label="@string/title_activity_device_list"
        android:theme="@style/AppTheme.NoActionBar" />

    <activity
        android:name=".MainInfoActivity"
        android:screenOrientation="landscape"
        android:theme="@style/AppTheme.NoActionBar" />

    <activity
        android:name=".DataSetupActivity"
        android:theme="@style/AppTheme.NoActionBar">

    </activity>
</application>

The Third Activity (MainInfoActivity) , it will change to the fourth Activity (DataSetupActivity) after I click the button , and the Third Activity (MainInfoActivity) did not close.

When I try to finish the fourth Activity (DataSetupActivity) and back to the Third Activity (MainInfoActivity) via called finish().

The screen orientation of Third Activity (MainInfoActivity) will change to the portrait and then change to the landscape. And the onCreate will perform 2 times.

I have reference the following link , it seem the bug.

How to prevent Screen Orientation change when Activity finishes on Android 8.1 Version Devices?

Does any method to solve this problem ? Thanks in advance.

Wun
  • 6,211
  • 11
  • 56
  • 101

2 Answers2

0

This is a reported bug: https://issuetracker.google.com/issues/69168442

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. Feel free to ask if something isn't clear.

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);
        }
    }
}
PerracoLabs
  • 16,449
  • 15
  • 74
  • 127
0

Try add this.

onCreate(){
   super.onCreate();
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}

onPause(){ 
  super.onPause();
  if (android.os.Build.VERSION.SDK_INT >= 27) {
         setRequestedOrientation(getResources().getConfiguration().orientation);
  }
}

onResume(){
  super.onResume();
  if (android.os.Build.VERSION.SDK_INT >= 27) {
         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
  }
}

Based on my answer here.

eytschkay
  • 961
  • 6
  • 9