你如何处理时,父活动的状态,势必要保存片段交易?如何处理、片段、状态

2023-09-08 09:58:30 作者:小怡妹纸.

我的应用程序有几个片段和活动。在主父活动的生命周期,应用程序presents信息/选项,以在其他活动用户的过程。

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():

注意:您可以提交使用提交()只之前的节能活动的状态(当用户离开该活动)的交易。如果试图在该点之后提交,一个异常将被抛出。这是因为,在提交之后的状态可以如果活动需要恢复丢失。以下情况时,它的好,你失去的提交,使用commitAllowingStateLoss()。

Caution: You can commit a transaction using commit() only prior to the activity saving its state (when the user leaves the activity). 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().

问题是返回到父活动后,我可以不再使用FragmentTransactions这是不可或缺的我已经在应用程序设计的导航方式。

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.

一个解决方案我想到的是我的活动变成碎片,但我的应用程序也将最终使用应用内结算,我相信总会开始了自己的活动。这似乎是一个巨大的限制 - 在发展的某个时候我就会有显示一个独立的活动

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.

我大概可以逃脱使用commitAllowingStateLoss(),但我觉得我缺少的Andr​​oid平板电脑应用开发的主要理念。是否有一个启动的活动,然后返回到父活动(负责管理碎片),而不会丢失提交FragmentTransactions能力的方法是什么?

I could probably get away with using commitAllowingStateLoss(), but I feel like I am missing a major concept in Android tablet app development. Is there a way to start activities then return to the parent activity (which manages fragments) without losing the ability to commit FragmentTransactions?

推荐答案

当你父活动恢复/启动,则可以再次提交片段的交易,即使它已经previous暂停。该文档仅意味着当活动被暂停(和国家已经保存),你不能在一段时间内提交,并已恢复再次之前。如果您访问另一个活动后返回到您的父活动,你可以自由地后,再次使用片段交易 Activity.onStart()被调用。

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.

这样做的原因的限制是Android的过程中 Activity.onSaveInstanceState节约与活动相关片段的状态()。如果您尝试了这一点后,使片段交易,那么你将接触到国家的损失,如果Android的需要重新+恢复活动(因为信息使用它来重新创建活动状态是只有在 Activity.onSaveInstanceState())。

The reason for this restriction is that Android saves the state of fragments associated with an Activity during 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()).