[英]fragment - best way to save/restore model after onDestroy of activity?
i have a fragment that is hosted inside of an activity. 我有一个活动内部托管的片段。 when user prsses the back button i need to save the model data and have it available the next time user opens the fragment/activity.
当用户按下后退按钮时,我需要保存模型数据,并在下次用户打开片段/活动时使它可用。 But just while in the app, it does not need to be persisted to disk.
但是就在应用程序中时,它不需要保留到磁盘上。 So for example if user destroyed the process, then there is no need to keep the model data, it can be fetched from network again.
因此,例如,如果用户破坏了该过程,则无需保留模型数据,则可以再次从网络中获取模型数据。
what i have tried: icePick and onSavedInstance
calls but these dont seem to kick in when user presses the back button on the fragment. 我曾尝试过: icePick和
onSavedInstance
调用,但是当用户按下片段上的“后退”按钮时,这些调用似乎没有启动。 tell me if im wrong. 告诉我我是否错了。
here is what i have implemented in my fragment: 这是我在片段中实现的内容:
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("myModel", Parcels.wrap(myModel));
}
i am using the parceler library if that makes any difference. 我正在使用包裹库,如果有任何区别。 I can also convert the code to kotlin if required.
如果需要,我还可以将代码转换为kotlin。 when i hit the back button the fragment gets popped off the stack and the activity contain it calls onDestroy but im not getting any call back in onSaveInstanceState.
当我按下“后退”按钮时,片段从堆栈中弹出,并且活动包含该片段,它调用onDestroy,但是即时通讯在onSaveInstanceState中没有得到任何调用。 Also when i check in
onCreate()
savedInstanceState
is null. 另外,当我签入
onCreate()
savedInstanceState
为null。 I have not overrided onSavedInstance
in the activity, just in the fragment. 我没有在活动中覆盖
onSavedInstance
,只是在片段中。 What am i doing wrong ? 我究竟做错了什么 ?
i had a though to use a database to do this, but i just need it while in memory and there should be a way to do this without a DB. 我曾经使用数据库来执行此操作,但是我只需要在内存中使用它即可,并且应该有一种无需数据库即可执行此操作的方法。
from what i learned if user hits the back button onSaveInstance is not called by the system: 从我学到的内容,如果用户点击后退按钮onSaveInstance,系统不会调用它:
If an activity is in the foreground, and the user taps the Back button, the activity transitions through the onPause(), onStop(), and onDestroy() callbacks.
如果活动位于前台,并且用户单击“后退”按钮,则该活动将通过onPause(),onStop()和onDestroy()回调进行转换。 In addition to being destroyed, the activity is also removed from the back stack.
除了被销毁之外,该活动还从后堆栈中删除。
It is important to note that, by default, the onSaveInstanceState() callback does not fire in this case.
重要的是要注意,默认情况下,onSaveInstanceState()回调在这种情况下不会触发。
#onSaveInstanceState
of fragment is strictly coupled to activity lifecycle 片段的
#onSaveInstanceState
与活动生命周期严格相关
According to doc 根据文档
Called to retrieve per-instance state from an activity before being killed
在被杀死之前被调用以从活动中检索每个实例的状态
You operates only with fragments and activity is left untouched, so this method is definitely can't be used in your case and shouldn't. 您只能使用片段操作,并且活动不会受到影响,因此绝对不能使用这种方法,也不应该这样做。
My suggestion is to use some kind of persistent storage though interface. 我的建议是通过接口使用某种持久性存储。 It could be in memory storage (any type of singleton, like suggested in comments. It may be scoped to app or activity or to custom case (you have to control manually cache lifecycle) and injected with dagger, for example), shared-preferences based storage, database storage.
它可以在内存存储中(任何类型的单例,如注释中建议的那样。它的范围可能限于应用程序或活动或自定义情况(例如,您必须手动控制缓存的生命周期),并使用匕首注入),共享首选项基于存储,数据库存储。 It is easy to test if you follow dependency injection patterns & use structural pattern like MVP (but it is not a point of this question)
测试是否遵循依赖性注入模式并使用MVP之类的结构模式很容易(但这不是这个问题的重点)
So store the data in the repository on change or in the onPause
method (because it is the last guaranteed to call when screen is being gone). 因此,将数据更改后存储在存储库中或
onPause
方法中(因为这是最后保证在屏幕消失时调用的数据)。 And restore it in onCreate
并将其还原到
onCreate
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.