处理方向更改时活动重启的更好方法?

发布于 2024-12-29 07:36:08 字数 578 浏览 3 评论 0原文

当用户离开某个活动时,该活动不应存在。这就是为什么它在 onStop 中有 finish() 方法。

@Override
protected void onStop() {
    super.onStop();
    finish();
}

然而,这使得每次屏幕方向改变时它都会重新启动。目前我通过 Manifest Activity 标签 android:configChanges="orientation" 和重写方法 onConfigurationChanged 处理此

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.someactivity);
    doAllInitializations();;
}

问题是否有更好的方法来处理这种情况?

There is an activity which should not exist when a user leaves it. That's why it has finish() method in the onStop.

@Override
protected void onStop() {
    super.onStop();
    finish();
}

However, this makes it restart each time the screen orientation changes. At the moment I handle this via Manifest Activity tag android:configChanges="orientation" and overriding method onConfigurationChanged

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    setContentView(R.layout.someactivity);
    doAllInitializations();;
}

Is there a better way to handle such situations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望笑 2025-01-05 07:36:08

您可以通过在 manifest 文件的 activity 标记 中添加 android:configChanges="orientation|keyboardHidden" 来停止此方向。

You can stop this orientation by adding android:configChanges="orientation|keyboardHidden" in your activity tag in the manifest file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文