onCreate() and onCreateView() invokes a lot more than required (Fragments)

前端 未结 6 1982
不知归路
不知归路 2021-01-30 10:19

Can somebody explain why the onCreate() and onCreateView() are being invoked so many times which increments with each orientation change?

Here

6条回答
  •  礼貌的吻别
    2021-01-30 11:11

    Yes, this is very bad documented. The explanation is that when the Activity is restored it will "auto-magically" restore the Fragments that were added in it, so adding another Fragment in your Activity will basically add another new Fragment on top of the previous Fragment\s that are actually restored by Android.

    This behavior is definitely intended, and the approach suggested by @Joris Wit is the correct one.

    Also this is very helpful when you think about it, because let's say you have a stack of Fragments added one on top of each other, and you can navigate back to them using the back key. In the case of a rotation, if Android would not restore the backstack of Fragments you will lose all of them, or you will have to implement some mechanism to keep track of your Fragment stack.

提交回复
热议问题