[英]Avoid setRequestedOrientation delay
I'm experiencing a small undesired effect when trying to set the orientation of my activity:尝试设置我的活动方向时,我遇到了一个小的不良影响:
I hace two activities: MainActivity and Activity2.我有两个活动:MainActivity 和 Activity2。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(v -> startActivity(new Intent(this, Activity2.class)));
}
}
public class Activity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
When I click the button with the device in portrait (but displaying the MainActivity in landscape) it loads the Activity2 in portrait and takes like 1 second to rotate to landscape.当我单击带有纵向设备的按钮(但以横向显示 MainActivity)时,它以纵向加载 Activity2,并需要 1 秒才能旋转到横向。
How can I avoid this and load the Activity2 with the correct orientation from the start?如何避免这种情况并从一开始就以正确的方向加载 Activity2?
NOTE: This is a simplified example.注意:这是一个简化的示例。 It is not possible to set up the orientation in the manifest since the orientation is not fixed and depends on the user input.由于方向不是固定的并且取决于用户输入,因此无法在清单中设置方向。
Thanks谢谢
Found a way to avoid the visual effect, but it is not elegant.找到了一种避免视觉效果的方法,但它并不优雅。
By declaring通过声明
public class Activity2 extends AppCompatActivity {
...
}
public class Activity2Land extends Activity2 {
...
}
And in the manifest在清单中
<activity
android:name=".Activity2"
android:screenOrientation="portrait"
android:exported="false" />
<activity
android:name=".Activity2Land"
android:screenOrientation="landscape"
android:exported="false" />
you can workaround this issue.您可以解决此问题。 But, yes, you need to declare A LOT of activities if you want to support reverse and sensor orientations.但是,是的,如果您想支持反向和传感器方向,您需要声明很多活动。
Any ideas for solving this in a correct way?以正确的方式解决这个问题的任何想法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.