簡體   English   中英

在Android 8.1中完成“活動”后如何解決屏幕方向變化?

[英]How to solve the screen orientation change when Activity finish in Android 8.1?

我正在8.1.0 Android手機上開發應用程序。 我有四個活動。 第一個是AskPermissionActivity ,它將在檢查許可后關閉並更改為Second Activity (DeviceListActivity)

當我在“ Second Activity (DeviceListActivity)選擇該項目時,它將更改為“ Third Activity (MainInfoActivity)

屏幕上的“ Third Activity (MainInfoActivity)方向” Third Activity (MainInfoActivity)為橫向,另一種為縱向。 類似於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>

Third Activity (MainInfoActivity) ,它將變為fourth Activity (DataSetupActivity) ,並且Third Activity (MainInfoActivity)沒有關閉。

當我嘗試完成fourth Activity (DataSetupActivity)並通過調用finish()返回Third Activity (MainInfoActivity) finish()

Third Activity (MainInfoActivity)的屏幕方向將更改為縱向,然后更改為橫向。 並且onCreate將執行2次。

我已經參考了以下鏈接,似乎是錯誤。

在Android 8.1版本的設備上完成“活動”后,如何防止屏幕方向發生變化?

請問有什么方法可以解決這個問題? 提前致謝。

這是一個已報告的錯誤: 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);
        }
    }
}

嘗試添加此。

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

根據我在這里的回答。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM