[英]Does Android delete local variables when the device is in sleep for a long time?
In my app i keep some data in local variables and sometimes after opening the app from sleep it reports a null pointer exception on local variable, which was existing before the device went to sleep.在我的应用程序中,我将一些数据保存在局部变量中,有时在从睡眠状态打开应用程序后,它会报告局部变量的空指针异常,该异常在设备进入睡眠状态之前就存在。 Is it possible that the system deletes these variables and how can i resolve this problem?
系统是否有可能删除这些变量,我该如何解决这个问题?
Probably you meant class fields, instead of local variables.可能你的意思是类字段,而不是局部变量。 In Android system View Life Cycle views such as Activities and Fragments can be destroyed if system needs more memory and application is in background.
在 Android 系统中,如果系统需要更多内存且应用程序在后台,则可以销毁活动和片段等视图生命周期视图。 When it happens few methods are called.
当它发生时,很少调用方法。 For example when activity is going to be cleared onPause(), onStop() and onDestroy() are called ( more ).
例如,当活动将被清除时 onPause()、onStop() 和 onDestroy() 被调用( 更多)。 You can override these methods and save current application state in SharedPreferences or in database.
您可以覆盖这些方法并将当前应用程序状态保存在 SharedPreferences 或数据库中。
Also you would need to check if you are not creating new Activity, if such happens, fields will be empty.此外,您还需要检查您是否没有创建新活动,如果发生这种情况,字段将为空。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.