android-fragments 当我更改设备方向时应用程序崩溃[已关闭]

iovurdzv  于 2022-11-14  发布在  Android
关注(0)|答案(2)|浏览(128)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
四个月前关门了。
Improve this question


我已经在fragment类中显示了这个graph,当我改变方向时,应用程序会崩溃。我如何在landscape模式下改变方向并显示图像而不崩溃应用程序。

4xy9mtcn

4xy9mtcn1#

当方向改变时,片段的状态也会随之改变。您需要通过调用

setRetainInstance(true);

onCreate()方法中。
您也可以使用onRestoreInstanceState()方法来储存您的savedInstanceState

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    onCreate(savedInstanceState);
}

最好的解决办法就是

android:configChanges="orientation|screenSize|keyboardHideen"

用于您的Activity,该Activity在清单文件中保存片段。

k75qkfdt

k75qkfdt2#

在方向改变时,android会破坏你的活动。也许你设置了一些东西,但它在改变时失效了。你可以在这里阅读更多信息:https://developer.android.com/guide/topics/resources/runtime-changes.html
您说您希望能够更改方向。为了完整起见,您可以将方向固定为横向或纵向,请参阅:Android - disable landscape mode?

相关问题