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

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

问题描述

我有一个使用片段的应用程序,我正在研究如何在具有双窗格的活动和作为独立活动的活动中使用相同的片段.仍然不确定完成此操作的最佳方法,但我注意到 FragmentManager 有一个 putFragment 和 getFragment 函数.让我感到困惑的是,您必须为 get 和 put 函数提供一个 Bundle 作为参数.不同的活动如何拥有相同的 Bundle?显然,您可以将 Bundle 作为参数传递,但那时我觉得您只是把事情弄得一团糟.

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 的好场景是什么?请包括 Bundle 参数说明.

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

推荐答案

基本答案:

这些仅在实现 onSaveInstanceState() 并在 onCreate() 中恢复该状态时才有用.如果您没有实现 onSaveInstanceState(),您可以忘记这些方法并假装它们不存在.

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.

他们正在解决的问题:如果您想在已保存的实例状态"中保存对片段的引用,则不能只在其中放置对象引用.首先是因为您不能将普通对象放入 Bundle 中.:) 这样做的原因是保存状态的意义在于将其复制出您的进程,因此如果您的进程需要被终止,则稍后可以将其复制回新进程供您重新- 初始化您的活动/片段.原始对象仅在它运行的进程的上下文中才有意义,因此无法正确地将对该对象的引用从当前进程复制到另一个进程.

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() 所做的是在给定的 Bundle 中放置一段数据,该数据可以将该片段识别到另一个进程中的活动/片段的新实例.这个表示究竟是什么,没有定义,但在当前的实现中,它是该片段的内部整数标识符,稍后将在 FragmentManager 需要从以前保存的状态重新创建该片段时使用......它是使用相同的标识符重新创建,因此当您调用 getFragment() 时,它可以检索整数,并使用它来确定正确的 Fragment 对象,以返回与先前保存的对象相对应的调用者.

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天全站免登陆