方向改变时碎片会导致崩溃 [英] Fragments leading crash when orientation changed

查看:112
本文介绍了方向改变时碎片会导致崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在片段类中显示了此 graph ,当我更改方向时,应用程序将崩溃.如何在不使应用程序崩溃的情况下更改方向并以 landscape 模式显示图像.

I have display this graph in fragment class and when I change the orientation app will crash. How can I change orientation and display image in landscape mode without crashing an app.

推荐答案

方向改变后,片段的状态也会改变.您需要通过调用来保存片段的状态

When the orientation change then the state of fragment will be changed. You need to save the state of your fragment by calling

setRetainInstance(true);

onCreate()方法中.

您还可以使用 onRestoreInstanceState()方法来存储您的 savedInstanceState

You can also use onRestoreInstanceState() method which will store your savedInstanceState

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

最好的解决方案是添加

android:configChanges="orientation|screenSize|keyboardHideen"

用于将片段保存在清单文件中的活动.

for your activity which holds the fragment in your manifest file.

这篇关于方向改变时碎片会导致崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆