碎片和方向变化 [英] Fragments and Orientation change

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

问题描述

什么是正确的方式使用片段时,处理一个方向变化?

我有一个包含2片景观布局(实例化code到的FrameLayout S)。当我切换到肖像模式(布局,其中只包含一个的FrameLayout 持有的左侧窗格中只),右手片段不再需要。

我收到一个错误:

  E / AndroidRuntime(4519):java.lang.IllegalArgumentException异常:由没有造成人员认为发现号0x7f060085的片段myFragment {418a2200#2 ID = 0x7f060085}
 

这是假设我的活动尝试重新连接的片段,其中它的方向改变之前,但因为包含片段在纵向模式引发该错误不存在的观点。

我曾尝试下面的隐藏/删除/分离的方法,但仍然得到错误。什么是正确的方式来告诉它不需要片段任何更多,不要试图显示?

@覆盖 公共无效的onCreate(包B){     super.onCreate(B);     片f = getSupportFragmentManager()findFragmentById(R.id.fragholder2)。     // rightPane是的FrameLayout保存我的片段。     如果(rightPane == NULL和放大器;&安培; F =空!){          FragmentTransaction英尺= getSupportFragmentManager()的BeginTransaction()。          ft.hide(F); //这不工作          ft.remove(F); //没有做到这一点          ft.detach(F); //或本          ft.commit;     } }

解决方案

我想我解决它。

我加了分片到后面堆栈中,然后才活动再次关闭弹出它关闭,有效地摆脱它。似乎工作至今。

What is the correct way to handle an orientation change when using Fragments?

I have a landscape layout that contains 2 fragments (instantiated in code into FrameLayouts). When I switch to portrait mode (the layout of which contains only one FrameLayout that holds the left pane only), the right hand fragment is no longer required.

I am receiving an error:

E/AndroidRuntime(4519): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f060085 for fragment myFragment{418a2200 #2 id=0x7f060085}

which is assume is my activity trying to re-attach the fragment where it was before the orientation change but as the view that contains the fragment does not exist in portrait mode the error is thrown.

I have tried the following hide/remove/detach methods but still get the error. What is the correct way to tell a fragment it is not needed any more and do not try to display?

@Override
public void onCreate(Bundle b) {
    super.onCreate(b);
    Fragment f = getSupportFragmentManager().findFragmentById(R.id.fragholder2);

    //rightPane is a framelayout that holds my fragment.
    if (rightPane == null && f != null) {
         FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
         ft.hide(f);     // This doesnt work
         ft.remove(f);   // neither does this
         ft.detach(f);   // or this
         ft.commit;
    }
}

解决方案

I think I resolved it.

I added the fragment to the back stack and then before the activity closes popped it off again which effectively gets rid of it. Seems to work so far.

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

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