Android 8.1中的Activity完成后如何解决屏幕方向的变化?

时间:2018-03-13 03:16:28

标签: android

我正在8.1.0安卓手机上开发应用程序。我有四个活动。第一个是Second Activity (DeviceListActivity),它会在检查权限后关闭并更改为Second Activity (DeviceListActivity)

当我选择Third Activity (MainInfoActivity)中的项目时,它将更改为Third Activity (MainInfoActivity)

<!-- 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> 的屏幕方向是横向,另一个是纵向。例如 AndroidManifest 中的以下代码:

Third Activity (MainInfoActivity)

fourth Activity (DataSetupActivity),单击按钮后,它将更改为Third Activity (MainInfoActivity),而fourth Activity (DataSetupActivity)未关闭。

当我尝试完成Third Activity (MainInfoActivity)并通过名为finish()返回Third Activity (MainInfoActivity)时。

onCreate的屏幕方向将更改为纵向,然后更改为横向。 catch将执行2次。

我参考了以下链接,它似乎是错误。

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

有什么方法可以解决这个问题吗?提前谢谢。

2 个答案:

答案 0 :(得分:0)

这是一个已报告的错误:https://issuetracker.google.com/issues/69168442

下一个解决方法可能会帮助您解决问题。

您必须使用代码中的一项来扩展所有活动。每当调用onPause / onResume方法时,它都会负责设置和恢复正确的方向。

该解决方法将适用于清单活动标签中定义的任何类型的定向。

出于我自己的目的,我从ComponentActivity扩展了该类,因此您可能需要更改为从Activity,ActivityCompat或您在代码中使用的任何类型的活动进行扩展。随时询问是否不清楚。

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

答案 1 :(得分:0)

尝试添加。

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

根据我的回答here