保留 Activity 之间的 Fragment 状态
是否可以在 Activity 之间保留 Fragment
?
假设我有 Activity A
,其中 Fragment F_Left
位于左侧,Fragment F_Right
位于右侧。如果我想启动一个新的 Activity 并保留 Fragment F_Left
...我该怎么做?
我可以在活动之间保留 Fragment F_Left
状态吗?
请注意,我想启动一个新的活动,因为片段 F_Left
是我的应用程序菜单,而片段 F_Right
完全更改了用户操作的上下文......并且我的应用程序有许多操作,因此每个操作都有一个活动是有意义的。
我知道可以在 Activity
中保留 Fragment
,但由于 Fragment
生命周期与容器 Activity
紧密相关> 我不知道是否可以在 Activity 之间保持 Fragment
状态。
Its possible to retain a Fragment
between Activities?
Lets say I have Activity A
with Fragment F_Left
placed at the left and Fragment F_Right
placed at the right. If I want to launch a new Activity and keep Fragment F_Left
... how can I do it?
Can I retain Fragment F_Left
state between activities?
Note that I want to launch a new Activity because Fragment F_Left
is my app menu and Fragment F_Right
changes completely the context of the user operations... and my app have many of operations, so it makes sense to have an Activity per operation.
I know its possible to retain Fragment
within an Activity
, but as Fragment
life cycle is closely tied to the container Activity
I don't know if this is possible keep Fragment
state between Activities.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
自 API 级别 13(
HONEYCOMB_MR2
,2011 年 6 月)开始,您可以跨活动保存和恢复片段的状态。要保存状态,请使用 < code>FragmentManager.saveFragmentInstanceState(),提供对要保存其状态的
Fragment
的引用。Fragment
必须在您尝试保存其状态时附加。要恢复状态,请使用
Fragment.setInitialSavedState()
为您激活同一个Fragment
时的返回值。您可以像保存任何其他对象一样在活动之间持久保存
SavedState
对象; 一种方法是对Application
进行子类化,如上所示(appState
是我们的子类的实例)。Since API Level 13 (
HONEYCOMB_MR2
, June 2011), you can save and restore the state of a fragment across activities.To save the state, use
FragmentManager.saveFragmentInstanceState()
, providing a reference to theFragment
whose state you wish to save. TheFragment
must be attached at the time you attempt to save its state.To restore the state, use
Fragment.setInitialSavedState()
with the return value when you instenciate the sameFragment
.You can persist the
SavedState
object across activities as you would any other object; one way is to subclassApplication
as shown above (appState
is the instance of our subclass).根据您对我的评论的回复,我的答案略有不同。对于您的具体情况,这可能不是最佳答案,我会让您决定。 :)
现在,您将片段捆绑在活动中,因为这对您来说有意义,但实际上,您可以将整个过程视为一个活动并使用 片段交易 隐藏&根据需要显示(或创建和销毁)片段。
由于您不会创建和销毁活动,因此左侧的菜单片段将保持不变,并且其 UI 状态不会有任何问题。您想要运行的操作集(无疑包括右侧的各种不同片段)不需要在新活动中启动 - 但您必须找到一种方法来管理片段所需的逻辑事务(在您的一个超级活动中或在某种 OperationsManager 类中)。
我认为这对于应用程序的用户来说最终会更加顺利,因为单个活动只是保持运行 - 并且您只需更改实际需要更改的部分。
Based on your response to my comment, I have a slightly different answer. It may not end up being the best answer in your specific situation, I'll let you decide that. :)
Right now you are bundling your fragments in activities because that is what made sense to you, but really, you can probably treat the entire process as one activity and use fragment transactions to hide & show (or create and destroy) fragments as needed.
Since you won't be creating and destroying activities, your menu fragment on the left will be left untouched, and you won't have any problems with its UI state. The set of operations you want to run (which no doubt includes all sorts of different fragments on the right) does not need to be launched in a new activity - but you will have to find a way to manage the logic you need for the fragment transactions (either in your one über-activity or in some kind of OperationsManager class).
I think this will end up being a lot smoother for the users of your application since the single activity just remains running - and you are only changing the parts that actually need to change.
不要启动新活动。
不是自动的。这不是同一个片段。您将在活动之间传递数据以供片段使用,这与没有任何片段时没有什么不同。
Don't launch a new activity.
Not automatically. It is not the same fragment. You would pass data between the activities for use by the fragment no differently than you would without any fragments at all.
为了可能回答您原来的问题,如果您触发另一个活动,那么我相信您可以通过在调用 onSaveInstanceState(...) 时调用 FragmentManager::putFragment(...) 来保存第一个活动中的片段,然后获取它稍后返回,例如在 onCreate(...) 中。
不过,我不得不同意Mark D的回应。
顺便说一句,我正在做类似的事情,因为我有一个双窗格设置,其中左窗格固定有多个选项,每个选项都会调用右窗格中的不同片段。此外,在右侧窗格中选择一个条目可能会导致右侧片段被另一个片段替换。
但是,我采取的方法是,左侧片段仅负责显示和处理右侧窗格中出现的即时片段的响应。此外,每个右侧片段负责用新片段“替换”自身并处理发送回它的结果。我使用 setTargetFragment、getTargetFragment 并调用目标片段的 onActivityResult 方法来传回结果。
对我来说,我采取的方法与我的应用程序在具有单个窗格的手机上运行时没有什么不同,其中初始选项的活动只知道它触发的活动,随后这些新选项会触发它们知道的进一步活动。
应该提到的是,除了加载左窗格片段之外,我在双窗格应用程序中的活动实际上并没有做太多事情,而且我不太明白单个活动是否需要管理数百个片段。
To potentially answer your original question, if you fire off another activity then I believe that you can save your fragment from your first activity by calling FragmentManager::putFragment(...) when onSaveInstanceState(...) is called and then getting it back later, e.g. in onCreate(...).
However, I have to agree with Mark D's response.
Incidentally I'm doing something similar in that I have a dual pane setup whereby the left pane if fixed with a number of options with each option invoking a different fragment in the right pane. Furthermore selecting an entry in the right pane can result in the right fragment being replaced by another one.
However, I have taken the approach whereby by left fragment is only responsible for displaying and handling responses from the immediate fragment which appears in the right hand pane. Furthermore each right-hand fragment is then responsible for 'replacing' itself with a new fragment and handling results sent back to it. I'm using setTargetFragment, getTargetFragment, and calling onto the target fragment's onActivityResult method to pass results back.
For me the approach I've taken is no different from when my app runs on a phone with a single pane whereby the initial option's activity only knows about the activies it fires off and subsequently these new ones fire off further activies which they know about.
It should be mentioned that my activity in my dual pane app doesn't really do much apart from loading the left pane fragment and I can't quite see the need for a single activity to ever have to manage hundreds of fragments.