简体   繁体   English

当父活动的状态必须保存时,如何处理片段事务?

[英]How do you handle fragment transactions when the state of the parent activity is bound to be saved?

My app has several fragments and activities. 我的应用程序有几个片段和活动。 Over the course of the main parent activity's lifecycle, the app presents information/options to the user in other activities. 在主要父项活动的生命周期中,该应用程序向其他活动中的用户显示信息/选项。

The documentation for Fragments has the following stipulation for commit(): Fragments的文档对commit()具有以下规定:

Caution: You can commit a transaction using commit() only prior to the activity saving its state (when the user leaves the activity). 警告:仅在活动保存状态之前(用户离开活动时),才可以使用commit()提交事务。 If you attempt to commit after that point, an exception will be thrown. 如果您在此之后尝试提交,将引发异常。 This is because the state after the commit can be lost if the activity needs to be restored. 这是因为如果需要恢复活动,则提交后的状态可能会丢失。 For situations in which its okay that you lose the commit, use commitAllowingStateLoss(). 对于可以丢失提交的情况,请使用commitAllowingStateLoss()。

The issue is that after returning to the parent activity, I can no longer use FragmentTransactions which are integral to the way I have designed navigation in the app. 问题在于,返回到父活动后,我将无法再使用FragmentTransactions,这些是我在应用程序中设计导航方式不可或缺的。

One solution I have thought of is to change my activities to fragments, however my app will also eventually use in-app billing which I believe will always start its own activity. 我想到的一种解决方案是将活动更改为片段,但是我的应用最终也会使用应用内结算,我相信它将始终启动自己的活动。 This seems like a huge restriction - at some point in development I will end up having to display a separate activity. 这似乎是一个巨大的限制-在开发的某个时刻,我最终将不得不展示一个单独的活动。

I could probably get away with using commitAllowingStateLoss(), but I feel like I am missing a major concept in Android tablet app development. 我可能可以使用commitAllowingStateLoss()逃脱,但是我感觉我缺少Android平板电脑应用程序开发中的主要概念。 Is there a way to start activities then return to the parent activity (which manages fragments) without losing the ability to commit FragmentTransactions? 有没有一种方法可以启动活动,然后返回到父活动(管理片段),而又不会失去提交FragmentTransactions的能力?

You can commit fragment transactions again when your parent activity is resumed/started, even if it has been previous paused. 当您的父活动恢复/启动时,即使先前已暂停,您也可以再次提交片段事务。 The docs only mean that you cannot commit during the period of time where the activity is paused (and the state has been saved) and before it has been resumed again. 该文档仅表示您不能在活动暂停(并且状态已保存)的时间内以及再次恢复之前提交。 If you return to your parent activity after visiting another activity, you are free to use fragment transactions again after Activity.onStart() has been called. 如果在访问另一个活动后返回到父活动,则可以在调用Activity.onStart()之后再次使用片段事务。

The reason for this restriction is that Android saves the state of fragments associated with an Activity during Activity.onSaveInstanceState() . 出现此限制的原因是,Android在Activity.onSaveInstanceState()期间保存了与活动关联的片段状态。 If you try to make fragment transactions after this point, then you will be exposed to state loss if Android needs to recreate + restore that Activity (since the information it uses to recreate the Activity state was only the data that was captured in Activity.onSaveInstanceState() ). 如果你尝试了这一点之后,使片段交易,那么你将面临亏损的状态,如果Android的需要重新+恢复活动(因为信息使用它来重新创建活动状态是只有在捕获的数据Activity.onSaveInstanceState() )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM