片段和屏幕方向变化
我不明白片段和屏幕方向应该如何在 Android 中工作。
我有一个用 XML 定义的片段。
创建该片段的成本很高,因此我想重新使用它。
我的代码如下所示:
@Override
protected void onSaveInstanceState (Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "myfrag", mFrag);
}
我
@Override
protected void onRestoreInstanceState (Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mFrag = getSupportFragmentManager().getFragment(savedInstanceState, "myfrag");
}
不明白的是如何用我重新获得的片段替换 XML 中的片段。当我进行屏幕方向更改时,它最终会破坏我的活动并重新创建它,但如果我可以保存我的片段并重用它,它应该会使方向更改更快。
蒂亚。
i do not understand how fragments and screen orientation is supposed to work in android.
I have a fragment defined in XML.
The fragment is expensive to create, so i want to re-use it.
I have code that looks like this:
@Override
protected void onSaveInstanceState (Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "myfrag", mFrag);
}
and
@Override
protected void onRestoreInstanceState (Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
mFrag = getSupportFragmentManager().getFragment(savedInstanceState, "myfrag");
}
What I don't understand is how to replace the fragment in XML with the fragment I've re-obtained. When i do a screen orientation change, it winds up destroying my activity and re-creating it, but if I can save my fragment and reuse it, it should make orientation changes a lot faster.
tia.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在 xml 中创建框架布局并在代码中创建片段。然后将创建的片段添加到您的框架布局中。在方向更改时,您尝试使用标签或 ID 查找创建的片段,并执行一些删除/添加操作。
类似的事情。
http://developer.android.com/guide/topics/fundamentals/fragments.html
看一下“或者,以编程方式将片段添加到现有 ViewGroup”小节。这是“向活动添加片段”部分的一个子部分。
亲切的问候,
布拉姆
You could create a framelayout in xml and a fragment in code. Then add the created fragment to your framelayout. On orientation change you try to find your created fragment by use of a tag or id and do some remove / add actions.
Something like that.
http://developer.android.com/guide/topics/fundamentals/fragments.html
Take a look at the sub-section "Or, programmatically add the fragment to an existing ViewGroup". This is a sub-section of the section "Adding a fragment to an activity".
Kind regards,
Bram