I have a BroadcastReceiver class. I have some static variables declared whose value is updated in side the onReceive() method. As per my knowledge static variable will keep it’s value across the onReceive calls. Is there any possibility when I will loose those values(Like my class will be unloaded resetting the static variables)? These are basically some temporary variables I need to be available for multiple onReceive calls.
I have a BroadcastReceiver class. I have some static variables declared whose value is
Share
From the documentation for BroadcastReceiver Lifecycle…
This isn’t going to make the use of static variables practical in the sense that things will be cleaned up quickly by the system. I’d try using
SharedPreferencesby calling…context.getSharedPreferences("MyReceiver", MODE_PRIVATE)…in the receiver’s
onReceive(...)method (replace"MyReceiver"with some name which makes sense to your app).