简体   繁体   English

活动与自定义视图中的onSaveInstanceState

[英]onSaveInstanceState in Activity vs Custom View

It seems that onSaveInstanceState when overridden in a custom View is not called if the activity that created the View also has onSaveInstanceState overridden. 如果创建视图的活动也具有onSaveInstanceState被覆盖,则在自定义View中被覆盖时,似乎不会调用onSaveInstanceState。 Why is this and is there a way to call both? 为什么会这样,有没有办法同时调用两者? Is one better to use than the other? 一个比另一个更好用吗? Since not all Views are custom I find that it might be necessary to have the activity call onSavedInstanceState too. 由于并非所有的View都是自定义的,因此我发现也有必要对活动进行onSavedInstanceState调用。

In addition if you call it in the Custom View, how do you tell the activity to not perform the time consuming tasks, upon rotation, that were used to create the View in onCreate in the first place? 另外,如果您在“自定义视图”中调用它,您如何告诉活动不要在旋转时执行最初用于在onCreate中创建视图的耗时任务? The onSaveInstanceState in the View was used to stop the activity from performing time consuming tasks again and again. 视图中的onSaveInstanceState用于阻止活动一次又一次执行耗时的任务。

You have to add super.onSaveInstanceState() in your overridden method, it calls this method in all attached fragments and views. 您必须在覆盖的方法中添加super.onSaveInstanceState() ,它会在所有附加的片段和视图中调用此方法。

It is a good practice to call super.onSaveInstanceState() every time you override it. 每次重写super.onSaveInstanceState()都是一个好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM