Android - where to keep global data?

 ̄綄美尐妖づ 提交于 2019-12-14 03:48:36

问题


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:


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




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



回答3:


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!



来源:https://stackoverflow.com/questions/14048722/android-where-to-keep-global-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!