Using the Android Application class to persist data

前端 未结 7 879
栀梦
栀梦 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."

    0 讨论(0)
  • 2020-11-22 09:03

    Dave, what kind of data is it? If it's general data that pertains to the application as a whole (example: user data), then extend the Application class and store it there. If the data pertains to the Activity, you should use the onSaveInstanceState and onRestoreInstanceState handlers to persist the data on screen rotation.

    0 讨论(0)
  • 2020-11-22 09:10

    You can actually override the orientation functionality to make sure that your activity isn't destroyed and recreated. Look here.

    0 讨论(0)
  • 2020-11-22 09:14

    I know this is the very old question but using the ViewModel from the jetpack components is the best way to preserve the data between Activity rotation.

    The ViewModel class is designed to store and manage UI-related data in a lifecycle conscious way. The ViewModel class allows data to survive configuration changes such as screen rotations.

    0 讨论(0)
  • 2020-11-22 09:19

    Those who count on Application instance are wrong. At first, it may seem as though the Application exists for as long as the whole app process exists but this is an incorrect assumption.

    The OS may kill processes as necessary. All processes are divided into 5 levels of "killability" specified in the doc.

    So, for instance, if your app goes in the background due to the user answering to an incoming call, then depending on the state of the RAM, the OS may (or may not) kill your process (destroying the Application instance in the process).

    I think a better approach would be to persist your data to internal storage file and then read it when your activity resumes.

    UPDATE:

    I got many negative feedbacks, so it is time to add a clarification. :) Well, initially I realy used a wrong assumption that the state is really important for the app. However if your app is OK that sometimes the state is lost (it could be some images that will be just reread/redownloaded), then it is fully OK to keep it as a member of Application.

    0 讨论(0)
  • 2020-11-22 09:19

    You can create Application class and save your all data on that calss for use that anywhere in your application.

    0 讨论(0)
提交回复
热议问题