1''
1''

Reputation: 27095

Android - where to keep global data?

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?

Upvotes: 4

Views: 2054

Answers (3)

codeMagic
codeMagic

Reputation: 44571

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

Upvotes: 1

Herry
Herry

Reputation: 7087

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">

Upvotes: 2

user529543
user529543

Reputation:

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!

Upvotes: 1

Related Questions