I have an android app, where there are couple of activities. Each of the activities download an xml/json feed, parse it and push that to a singleton usually as an arraylist.
However after going through various activities, It seems the singleton is dying and most of the earlier downloaded array lists are now empty. Why is it so? Isn't singleton a recommended practice for sharing data across activities?
Adding my comment as an answer
If you want to keep a global variable across activities then keep that in Application. Extend a class from Application and add that in your AnddroidManifest file.
From the Android API FAQ shared by @rekaszeru
Singleton class
You can take advantage of the fact that your application components run in the same process through the use of a singleton. This is a class that is designed to have only one instance. It has a static method with a name such as getInstance() that returns the instance; the first time this method is called, it creates the global instance. Because all callers get the same instance, they can use this as a point of interaction. For example activity A may retrieve the instance and call setValue(3); later activity B may retrieve the instance and call getValue() to retrieve the last set value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With