4

I have several 2D and 3D arrays that I'd like to access from multiple places within my app, which will be loaded from SharedPreferences as strings when my app starts and saved back to SharedPreferences whenever onPause() is called on any activity. It's too tedious to pass them between activities with intents or bundles. Is there any way to store data globally in Android?

1''
  • 26,823
  • 32
  • 143
  • 200

3 Answers3

2

If i need to store some data in SharedPreferences and that need to access multiple place in my application then i do code like below one.

public class YourClassName extends Application{


    private static YourClassName mClassInstance;

    public static SharedPreferences mSharedPreferences;

    public static Editor mEditor;


    public static String KEY_DB_STATE="DbStateKey";

    public static String SHARED_PREFERENCE_NAME="YourClassNamePref";

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    public void onCreate() {
        super.onCreate();

        mClassInstance=this;
    }


    public static YourClassName getInstance(){
          checkAppCreated();
        return mClassInstance;
    }



    public static void checkAppCreated(){
         if (mClassInstance == null)
                throw new IllegalStateException("Application not created yet!");
    }



    @Override
    public void onLowMemory() {
        super.onLowMemory();
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }


    public static void setDatabaseState(String sharedPreferenceName,boolean state){

        mSharedPreferences=getInstance().getSharedPreferences(sharedPreferenceName, Context.MODE_PRIVATE);
        mEditor=mSharedPreferences.edit();
        mEditor.putBoolean(KEY_DB_STATE, state);
        mEditor.commit();

    }


    public static boolean getDatabaseState(String sharedPreferenceName){
        mSharedPreferences=getInstance().getSharedPreferences(sharedPreferenceName, Context.MODE_PRIVATE);
        return mSharedPreferences.getBoolean(KEY_DB_STATE, false);
    }



}

NOTE:

Don't forgot to put your Application class in android manifest file like below one for my class.

<application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" android:name="com.android.YourClassName">
Herry
  • 7,037
  • 7
  • 50
  • 80
1

I am storing in MyApplication class, when I need to have globally. Almost from everywhere you can get reference to application / application context.

Memory leak warning: long live object in short live collections ( like activities) at least my app is living most of the time!

  • 1
    I think this is what I need, also. We keep these things in a Globals class where most things are static and I don't think this is right. I inherited this code and am trying to clean it up. Is it safe to put methods there that are used by many activities or should they be in separate classes? – codeMagic Dec 27 '12 at 02:54
  • I hadn't thought about making a class with everything static. It sounds pretty good, actually. What's wrong with global static methods? – 1'' Dec 27 '12 at 03:00
  • is safe, just keep it as much as you can the code readable. ( not all the 100 utility functions there) There are a few extra case, when MyApplication is destroyed, but a part of it is still living. Is related to security, in normal cases the MyApp is the best place, or from there get a holder to your class. –  Dec 27 '12 at 03:01
  • It may be ok if you don't use it too much but we have a lot of code in it. When I made that suggestion to two different posts everyone got upset and said not to do it but no one ever answered as to why not. – codeMagic Dec 27 '12 at 03:02
  • @1 with static methods the problem is: can't figure out the context where are they: if can be even in an activity. Try to set the background for an Activity (that will make a reference back) and is ready the memory leak: your app will crash after a few rotations with OutOfMemoryError –  Dec 27 '12 at 03:03
  • @codeMagic I'd like to accept your answer about using static fields in a Globals class. This helped me tremendously! – 1'' Dec 27 '12 at 05:21
1

What we have done, is used a Globals class that uses mostly static fields and static methods, though not all are static. These are mostly fields and methods that may be used by many different classes/activities in the app. The problem with this is to not allow it to grow out of control. Every once in awhile, I go through and see what can be taken out and put in classes together or moved to an existing class. Sometimes there is the problem in which you need application context also which I have resolved by passing context in certain situations or creating a method that gets the application's context. There may be problems with memory leaks but I have not had any issues thus far, although that doesn't mean I won't.

I have been told not to do this and that it isn't OOP but I think that is incorrect and no one has told me why this is wrong yet. You still create objects and follow OOP standards. Glad I could help

codeMagic
  • 44,549
  • 13
  • 77
  • 93