当使用FragmentManager :: putFragment和getFragment [英] When to use FragmentManager::putFragment and getFragment

查看:1002
本文介绍了当使用FragmentManager :: putFragment和getFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用碎片的应用程序,我是用如何使用相同的片段的活性的双窗格和活动单独订购玩耍。仍不能确定在完成这一点,但我注意到FragmentManager有putFragment和getFragment功能的最佳方法。什么困惑我的是,你必须提供一个捆绑作为参数传递给get和put函数。如何可以单独活动具有相同的包?很明显,你可以通过捆绑作为参数,但在这一点上我觉得你只是让乱七八糟的东西。

I've got an application that uses fragments and I was playing around with how to use the same fragment in an Activity with a dual pane and an Activity as a stand alone. Still not sure on the best method for completing this but I noticed the FragmentManager has a putFragment and getFragment function. What confuses me is that you have to provide a Bundle as parameter to both get and put functions. How can separate Activities have the same Bundle? Obviously you could pass the Bundle as a parameter but at that point I feel like you're just making a mess of things.

那么,什么是一个很好的方案,使用getFragment和putFragment?请包括捆绑的参数说明。

So what is a good scenario for using getFragment and putFragment? Please include the Bundle parameter explanation.

推荐答案

最基本的答案:

实施时的onSaveInstanceState(),并且恢复状态的onCreate(),这是唯一有用的。如果你没有实现的onSaveInstanceState(),你可以忘掉这些方法和pretend好像他们不存在。

These are only useful when implementing onSaveInstanceState() and restoring that state in onCreate(). If you are not implementing onSaveInstanceState(), you can forget about these methods and pretend like they don't exist.

他们正在解决的问题:如果要引用保存到一个片段的保存的实例的状态,你不能只是把对象的引用在那里。首先,因为还有你不能把普通的对象包。 :)而这样做的原因是,该保存状态的点是,它复制了你的程序,所以如果你的进程需要被杀死,以后就可以复制回以一个新的进程,为您重新-initialize从你的活动/片段。甲生对象是唯一有意义在它是在运行过程中的情况下,所以这是不可能的基准正确复制到这样一个对象自己当前的进程,并在另一

The problem they are solving: if you want to save a reference to a fragment in your "saved instance state," you can't just put an object reference in there. First because well you can't put plain object in a Bundle. :) And the reason for this is that the point of that saved state is for it to be copied out of your process, so if your process needs to be killed, it can later be copied back in to a new process for you to re-initialize your activity/fragment from. A raw object is only meaningful in the context of the process it is running in, so it isn't possible to correctly copy the reference to such an object out of your current process and in to another.

那么putFragment()/ getFragment()做的是放置一个片在给定的包数据可以跨识别片段的活性/片段中另一个进程的新实例的。正是这种重presentation是,没有被定义,但在当前的实现它是该片段的内部整数标识符,这将在使用后,当FragmentManager需要重新创建从一个$ P $该片段pviously保存的状态......这是与相同标识符重新创建,所以当你再调用getFragment(),它可以检索整数,并用它来确定正确的片段对象返回给对应于一个被调用者previously保存。

So what putFragment()/getFragment() do is place a piece of data in the given Bundle that can identify that fragment across to a new instance of your activity/fragment in another process. Exactly what this representation is, is not defined, but in the current implementation it is the internal integer identifier for that fragment, which will be used later when the FragmentManager needs to re-create that fragment from a previously saved state... it is re-created with that same identifier, so when you then call getFragment() it can retrieve the integer, and use that to determine the correct Fragment object to return to the caller that corresponds to the one that was previously saved.

这篇关于当使用FragmentManager :: putFragment和getFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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