简体   繁体   English

设备长时间休眠时Android是否会删除局部变量?

[英]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.

相关问题 如何在设备处于睡眠模式的某个特定时间在Android中推送本地通知 - How to Push Local Notification in android at some particular time when device is in sleep mode 试图使Android设备进入睡眠状态,但是PowerManager不包含“ goToSleep(long)方法” - Attempting to put Android device to sleep, but the PowerManager does not contain a "goToSleep(long) method 当手机处于睡眠模式或长时间隐藏应用时,Android后台服务的运行速度较慢 - Android background service works slower when phone in sleep mode or app hidden for a some long time 设备休眠时,Android应用停止工作 - Android app stop to work when device sleep 有谁知道如何从Android 2.1设备获取本地日期和时间? - Does anyone know how to get the local date and time from Android 2.1 device? 如何在特定时间使我的android设备进入睡眠状态并以编程方式在其他时间唤醒? - How to sleep my android device at an specific time and wake up at some another time programmatically? Android:当睡眠或等待不起作用时,如何在方法中设置时间间隔 - Android: How to set time gap in methods, when sleep or wait is not working 在 Android Studio 中重新分配时局部变量下划线 - Local variables underline when reallocating in Android Studio 为什么当设备进入睡眠模式时前台服务停止工作 - Why does foreground service stop working when device go into sleep mode 设备处于睡眠模式时URLConnection不起作用 - URLConnection when device is in sleep mode not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM