In the following code i have a Class that contains a bunch of variables. I create a couple of instances of that class and save them to an arraylist. The arraylist is part of the same class, but marked static. The same app has a homescreen app widget. I worry that the static arraylist will stay in memory forever until the user deletes the app widget. Is this true? Is the static arraylist a good idea or what could be an alternative?
public class Data {
public static ArrayList<Data> listData = new ArrayList<Data>();
String string;
int integer;
Data(String string, int integer){
this.string = string;
this.integer = integer;
}
}
public class DataChanger {
DataChanger(){
Data.listData.add(new Data("string", 1));
}
}
static arraylist will stay in memory forever until the user deletes the app widget
That is unlikely to occur, since the AppWidgetProvider
is a convenience BroadcastReceiver
class for Android widgets. If nothing else of your application is running, your process may be terminated between onUpdate()
calls.
The official docs for BroadcastReceiver
make it very clear that
A
BroadcastReceiver
object is only valid for the duration of the call toonReceive(Context, Intent)
. Once your code returns from this function, the system considers the object to be finished and no longer active.This has important repercussions to what you can do in an
onReceive(Context, Intent)
implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point theBroadcastReceiver
is no longer active and thus the system is free to kill its process before the asynchronous operation completes.In particular, you may not show a dialog or bind to a service from within a
BroadcastReceiver
. For the former, you should instead use theNotificationManager
API. For the latter, you can useContext.startService()
to send a command to the service.
References - Reference 1, Reference 2, Reference 3