Which one to use: onSaveInstanceState vs. onRetainNonConfigurationInstance?

后端 未结 1 1950
夕颜
夕颜 2020-12-02 07:31

As far as I can see onRetainNonConfigurationInstance is a redundant callback. If my activity has really expensive initialization, I am better off using onSaveInstanceState.

相关标签:
1条回答
  • 2020-12-02 08:08

    As far as I can see onRetainNonConfigurationInstance is a redundant callback.

    No, it is not.

    If my activity has really expensive initialization, I am better off using onSaveInstanceState.

    onSaveInstanceState() is not designed for "really expensive initialization". It is designed for "hey, the user made some changes to the information in the activity but has not saved it yet, let's not lose that data, m'kay?".

    Is there any guideline for using one API vs. the other?

    If it fits in a Bundle and is not too big, use onSaveInstanceState(). Everything that does not fit in a Bundle (e.g., a socket) or is really big (e.g., a photo as a Bitmap) should use onRetainNonConfigurationInstance(), and your application should be in position to re-create those items if needed.

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