方向上的开关活动已更改



旋转手机时如何切换到另一个活动?

我的要求:

  • 活动A和B仅为纵向
  • 酸性L仅为景观
  • 当显示A或B的横向模式时,L被启动。当将显示L的肖像时,显示A或B

我可以创建这种行为,除了后退按钮。当按下它时,我要么得到横向的A/B,要么得到纵向的L,这是我想防止的。

我在做什么(活动A和B,但L相似(

在方向改变时触发活动调用:

@Override
public void onConfigurationChanged(Configuration newConfig) {
int newOrientation = newConfig.orientation;
if (newOrientation == Configuration.ORIENTATION_LANDSCAPE) {
Intent intent = new Intent(this, ActivityL.class);
startActivity(intent);
}
super.onConfigurationChanged(newConfig);
}

我想要下面这样的东西。但是手动(重新(设置方向会阻止调用onConfigurationChanged((:

@Override
public void onResume(){
super.onResume();
int currentOrientation = this.getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}

当然,如果我在其他生命周期方法或onCreate(…(中这样做,也会发生同样的情况

所有活动都有:

android:screenOrientation="sensor"

我还试着用旋转角度来模仿方向的变化,但这种行为似乎大多是随机的,甚至不接近正常的方向变化。

我想setRequestedOrientation((会覆盖我在AndroidManifest:中定义的内容

android:screenOrientation="sensor"

我需要将其重置为传感器,以恢复自动方向更改:

@Override
public void onConfigurationChanged(Configuration newConfig) {
int newOrientation = newConfig.orientation;
if (newOrientation == Configuration.ORIENTATION_LANDSCAPE) {
Intent intent = new Intent(this, ActivityL.class);
startActivity(intent);
}
super.onConfigurationChanged(newConfig);
// Reset to sensor:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
}

我有点惊讶在onConfigurationChanged(..(中调用它是有效的,因为该方法不再由方向更改调用。但我想,当活动恢复时(按下后退按钮时(,配置会以其他方式更改。

最新更新