Storing variables through onDestroy() event

醉酒当歌 提交于 2019-12-24 21:22:00

问题


I'm looking for a way to store the state of my variables that may have been changed from there initiation variable (ever by user activating a function or other) through the onDestroy() event so that if i turn my phone on and off my app hasn't reset the variables.


回答1:


First of all, this is from android reference: "Note: do not count on onDestroy method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle)"

For saving variables you can use as said before SharedPreferences.

Example for using inside activity class:

SharedPreferences prefs = getSharedPreferences("preference_file_name", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("variable_key", variable);
editor.commit();

For method onSaveInstanceState(Bundle) just use Bungle argument to save variables




回答2:


Look at the SharedPreferences feature. It is designed just for this case. Good sites to read are here, this and this. And look at this question.



来源:https://stackoverflow.com/questions/13088981/storing-variables-through-ondestroy-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!