Using the Android Application class to persist data

前端 未结 7 1063
栀梦
栀梦 2020-11-22 08:55

I\'m working on a fairly complex Android application that requires a somewhat large amount of data about the application (I\'d say a total of about 500KB -- is this large fo

7条回答
  •  故里飘歌
    2020-11-22 09:01

    I don't think 500kb will be that big of a deal.

    What you described is exactly how I tackled my problem of losing data in an activity. I created a global singleton in the Application class and was able to access it from the activities I used.

    You can pass data around in a Global Singleton if it is going to be used a lot.

    public class YourApplication extends Application 
    {     
         public SomeDataClass data = new SomeDataClass();
    }
    

    Then call it in any activity by:

    YourApplication appState = ((YourApplication)this.getApplication());
    appState.data.UseAGetterOrSetterHere(); // Do whatever you need to with the data here.
    

    I discuss it here in my blog post, under the section "Global Singleton."

提交回复
热议问题