[英]Android 8.1 screen orientation issue: flipping to landscape a portrait screen
我以縱向模式進行所有活動,除了我用來播放始終為風景的視頻的活動。 我發現在Android 8.1上每次打開視頻活動並關閉它之前的活動都會轉到橫向,即使它在清單上設置為“縱向”。
這只發生在從景觀活動回來的時候。
有人在體驗這個嗎?
謝謝。
編輯
我在Google上報告了該錯誤: https : //issuetracker.google.com/issues/69168442
編輯2
它似乎在Android 9上修復
剛剛在我自己的應用程序中遇到了這個問題。
適合我的解決方案如下:
onCreate(){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
onPause(){
if (android.os.Build.VERSION.SDK_INT >= 27) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
}
onResume(){
if (android.os.Build.VERSION.SDK_INT >= 27) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
}
}
上面的代碼應該進入橫向模式的活動(即第二個活動,以及按下后退按鈕的活動)
我想指出這個解決方案不是我自己的,我從以下鏈接的#20帖子中獲取了它(也在OP中注明):
https://issuetracker.google.com/issues/69168442
我只是覺得如果人們不必搜索其他頁面就可以更容易訪問。
這解決了這個問題。
覆蓋Landscape活動的onBackPressed()方法並將方向設置為Portrait 。
@Override
public void onBackPressed() {
super.onBackPressed();
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
在之前的Activity中使用intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK)
解決了我的問題。
如果您在宣言中有類似theme = Theme.AppCompat.Light.Dialog的DialogTheme,請從manifesto中刪除該Dialog活動的set orientation標記,它將從上一個活動中獲取Orientation並將setorientation標記用於重新激活活動
對於以下版本,請將其放在oncreate上
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
並為應用程序設置主題Theme.AppCompat.Light.DarkActionBar無需向活動添加主題
如果您需要支持父級活動的方向更改,請考慮使用橫向活動的onPause()中的當前方向。
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);
}
}
這個答案基於TheTestSpecimen的答案。
來自Narmi的回答:
當您從活動B返回活動A並且如果您知道活動A的屏幕方向時,請將屏幕方向設置為活動B的ondestroy。
您必須檢測活動是否正在破壞配置更改,因此添加字段isConfigurationChanged = false
,然后在onSaveInstanceState
方法上將其轉為true
並在onDestroy
方法上添加以下內容:
@Override protected void onDestroy() { if(!isConfigurationChanged) setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); super.onDestroy(); }
在每個活動的onCreate方法中插入此行
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
對於景觀和
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
對於肖像
我有這樣的問題,並嘗試了以上所有用例。 他們中的大多數都有效,但有一個案例你應該知道:
最后的原因是在Android 8的情況下在活動內容布局中使用片段 。 例如:活動以橫向模式啟動,但片段顯示縱向布局。
盡量避免碎片。
要解決此問題:
當您從活動B返回活動A並且如果您知道活動A的屏幕方向時,請將屏幕方向設置為活動B的ondestroy。
@Override
protected void onDestroy() {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onDestroy();
}
下一個解決方法可以幫助您解決問題。
您必須使用代碼中的活動擴展所有活動。 無論何時調用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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.