Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store Objects in ApplicationContext

Tags:

android

When my application goes to background , my (static and singleton) objects are cleared. So I tried to store these objects in Applicaton Context . I am using the following code.

Accounts.create(getApplicationContext()) will be called once to store the accounts instance.

Is that possible(reliable) to store objects in Application Context ? I am not sure the following way is correct or not . please guide ..

   public class Init extends Application {
    private Hashtable<Object, Object> globalStore = new Hashtable<Object, Object>();

    public void putToGlobalStore(Object key, Object value) {
        globalStore.put(key, value);
    }

    public Object takeFromGlobalStore(Object key) {
        return this.globalStore.get(key);
    }

    public void removeFromGlobalStore(Object key) {
        this.globalStore.remove(key);
    }

    public boolean containsInGlobalStore(Object key) {
        return this.globalStore.containsKey(key);
    }
}

public class Accounts {

        protected Accounts(String name, Context context) {
            Init init = (Init) applicationContext;
                init.putToGlobalStore(name, this);
        }

        private static Init applicationContext;

        public static void create(Context context) {

            if (context instanceof Application)
                applicationContext = (Init) context;
            else
                applicationContext = (Init) context.getApplicationContext();

            if (applicationContext.containsInGlobalStore(GLOBAL_NAME))
                Logger.log("Warning " + GLOBAL_NAME
                        + " is already created. This will remove all old datas");

            new Accounts(GLOBAL_NAME, applicationContext);


        }

        private static final String GLOBAL_NAME = "accounts";

        public static Accounts getInstance() {

            try {
                return (Accounts) applicationContext
                        .takeFromGlobalStore(GLOBAL_NAME);
            } catch (Exception e) {
                Logger.log("GLOBAL_NAME Lost");
                return null;
            }

        }

Please help.

like image 502
Harikrishnan R Avatar asked Apr 20 '11 05:04

Harikrishnan R


3 Answers

You should know that the application context itself gets destroyed if left unused for a long time in the background. So there is no guarantee that your static and singleton objects will not be cleared when the app is in background. Instead what you can do is persist your objects from time to time (either in a flat-file or shared preference or database) and restore them in the onCreate method of the Application class

like image 196
pankajagarwal Avatar answered Oct 05 '22 23:10

pankajagarwal


I know this question was asked a long time ago, but here's a good article that suggests using the Application object to store data is generally not a sound design methodology.

like image 33
tronman Avatar answered Oct 05 '22 22:10

tronman


I have been using this method in my application and i didn't see any problem unless my process gets killed by the OS or if there is a crash in my application and my app gets restarted.

If you think whatever data you are storing is valid for only life time of a program why don't you override OnCreate of Application object and create all your singletons there. This way you can always make sure your application has all singletons before your app starts functioning.

like image 44
Naresh Avatar answered Oct 05 '22 23:10

Naresh