简体   繁体   English

避免 setRequestedOrientation 延迟

[英]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.

相关问题 更改方向而不设置setRequestedOrientation - Change orientation without setRequestedOrientation 如何避免GoogleAppEngine数据存储延迟? - How can i avoid GoogleAppEngine DataStore delay? Android - setRequestedOrientation - 活动和视图生命周期 - Android - setRequestedOrientation - Activity and views lifecycle 清单screenOrientation属性与setRequestedOrientation - Manifest screenOrientation property vs setRequestedOrientation 使用setRequestedOrientation()时出现黑屏 - getting a black screen when using setRequestedOrientation() mediaplayer.prepare,如何避免吐司延迟。 “加载中” - mediaplayer.prepare, How to Avoid Toast delay. “Loading” setRequestedOrientation无法在react-native中找到符号 - setRequestedOrientation cannot find symbol in react-native 如何避免不必要的鼠标滚动事件并在延迟后执行代码 - How to avoid unwanted Mouse Scroll Event and execute code after a delay 单击按钮后,setRequestedOrientation后自动旋转不起作用 - Auto rotate doesn't work after setRequestedOrientation when button is clicked 快速切换两个按键时如何避免按键绑定延迟 - How to avoid the key binding delay when alternating quickly between two keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM