简体   繁体   English

片段-onDestroy活动后保存/恢复模型的最佳方法?

[英]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. 我曾尝试过: icePickonSavedInstance调用,但是当用户按下片段上的“后退”按钮时,这些调用似乎没有启动。 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()回调在这种情况下不会触发。

source: here 资料来源: 这里

#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.

相关问题 片段的onActivityCreated()在Activity的onDestroy()之后调用 - Fragment's onActivityCreated() is called after onDestroy() of Activity 保存来自Edittext的最新文本,并在onDestroy之后将其还原 - Save latest text from Edittext and restore it after onDestroy 在活动上保存/恢复片段堆栈已损坏 - save/restore fragment stack on Activity destroyed 销毁并重新创建活动后处理重新创建的对话框片段的最佳方法 - Best way of handling recreated dialog fragment after activity is destroyed and recreated 调用另一个Activity后删除片段的最佳方法是什么? - What is the best way to remove fragment after calling another Activity? 调用onDestroy后在AsyncTask.onPostExecute中处理FragmentTransaction的最佳方法 - Best way to Handle FragmentTransaction in AsyncTask.onPostExecute after onDestroy is called 保存和恢复控制命令的最佳方法是什么 - What is the best way to save and restore the controlling commands 最佳方式:在 ScrollView 中保存和恢复 TextView 位置 - Best Way: Save & Restore TextView Position in ScrollView 从后台弹出活动时保存并恢复片段 - Save and restore fragment when activity is popped from back stack 如何在Android中正确保存和还原“活动”和“片段”状态? - How to properly save and restore Activity and Fragment status in Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM