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

前端 未结 1 402
夕颜
夕颜 2021-01-18 03:15

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.

相关标签:
1条回答
  • 2021-01-18 03:30

    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.

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

    0 讨论(0)
提交回复
热议问题