0

I am trying to use a Singleton to share a large data object between Activities. But when I open the new Activity, the singleton comes up as empty. It seems to me that the Singleton should be the same no matter where in the Application I call if from.

It seems like the Scope of the Singleton is being limited to the individual Activity. Working around this is making my App very complicated. I must be doing something wrong. I even tried instantiating them in an extended Application class... Google says I should not have to use that though...

Can someone please point out where I am going wrong? i.e. Why does this singletom not contain the same data in each Activity?

I call it from an Activity with...

DataLog dataLog = DataLog.getInstance(this);

I have...

public class DataLog extends ArrayList<String> implements Serializable {
    private static final long serialVersionUID = 0L;
    private static DataLog sInstance;
    private static Context mContext;

    public static DataLog getInstance(Context context) {
        mContext = context.getApplicationContext();
        prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
        if (sInstance == null) {
            sInstance = new DataLog();
        }
        return sInstance;
    }

    private DataLog() {
    }

    public boolean add(String entry) {
        super.add(entry);
        return true;
    }

    public void add(int index, String entry) {
        if (index > 0)
            super.add(index, entry);
        else
            super.add(entry);
    }

    public void clear() {
        super.clear();
    }

    ...
}
PrecisionPete
  • 3,139
  • 5
  • 33
  • 52
  • I'd call that a feature, not a issue. You really shouldn't be operating on objects between threads. That's a well known pitfall. – stephen Aug 17 '13 at 17:17

1 Answers1

0

Its highly advisable to avoid singleton for sharing large data sets in android.

Singletons are used for short life-cycle objects.

Switch to SharedPrefferences, SQLite DB's or file storing. You are not the only to have experienced this behavior, and the reason lies in the nature of android Activities and the system itself(managing activities and its data).

Here is an example why singleton is bad for your case:

You stored important data in it. The user knows that he can close the app on home button to call someone or whatever)maybe someone called him when he was in your app), and that when he opens your app he will come back at the same place with everything in order. (this is expected behavior from users and android apps). The system can easily kill your process and all static variables in it for memory maintenance, app inactivity etc...result=data lost. Thus its not safe to use it.

JanBo
  • 2,925
  • 3
  • 23
  • 32
  • Similar questions: http://stackoverflow.com/questions/10623160/problems-with-singleton-in-android http://stackoverflow.com/questions/7503997/when-why-does-my-java-singleton-instance-get-destroyed – JanBo Aug 17 '13 at 17:15
  • I was starting to suspect something like that. Thanks for clearing it up. I'll stop beating my head against the wall now... This is quite an architectural difference from Blackberry 7 - what I'm used to. Thanks – PrecisionPete Aug 17 '13 at 21:26