views:

83

answers:

2
public class MYApplication extends Application {
   String property;

setter getter

}

does above code make sure property will not be collectied by android OS if not used for a long period of time.

+3  A: 

No. Android reserves the right to kill any application at any time if it feels the need. Even foreground processes can be killed if the device gets low on memory. This means the Application object will be destroyed and all its attributes lost.

The only way to ensure your application's transient state never gets lost is to respond appropriately to the lifecycle events Android offers, or just store values persistently.

If you want to store a String for your application why not use Preferences? This means the value would be never be lost, even if the device is switched off.

private static final String PREFERENCE_MYSTRING = "mystring";

static String getProperty(Context context) {
    return PreferenceManager.getDefaultSharedPreferences(context)
        .getString(PREFERENCE_MYSTRING, "");
}

static void setProperty(Context context,String value) {
    PreferenceManager.getDefaultSharedPreferences(context)
        .edit()
        .putString(PREFERENCE_MYSTRING, value)
        .commit();
}

(I'm not quite sure what getter and setter are in your code sample, but I don't think that's relevant.)

Dave Webb
A: 

As I understand it, it's central to override onDestroy() if you want to prevent Android OS from killing your process.

Christian