开发者

Which one to use: onSaveInstanceState vs. onRetainNonConfigurationInstance?

As far as I can see onRetainNonConfigurationInstance is a redundant callback. If my activity has really expensive initialization, I am better off using onSaveInstanceState. Saved instance covers more situation开发者_如何转开发s than non-configuration instance. Is there any guideline for using one API vs. the other? Thanks.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜