Using the Android Application class to persist data

前端 未结 7 1055
栀梦
栀梦 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: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.

提交回复
热议问题