简体   繁体   中英

How to write to and read data from Android's ApplicationContext?

In my app I need a central storage object that will be accessed from different parts of the application (like a singleton data holder).

AFAIK the clean way to implement singletons in Android is to use the ApplicationContext .

How can I

  1. put data (like instance of List<MyPieceOfInformation> ) in the ApplicationContext and
  2. get them out of it

?

Is it correct that the only way to store more or less complex data in Android is to use the built-in SQLite database?

you can use mysql and others as well. it is all depends if you want to save the data in local or external. as external, for example, you can use mysql and web server, then communicate using json.

for saving List, you can use static.

In my app I need a central storage object that will be accessed from different parts of the application (like a singleton data holder).

Then use a singleton.

AFAIK the clean way to implement singletons in Android is to use the ApplicationContext.

First, there is nothing in Android named ApplicationContext . You probably mean Application .

Second, in the opinion of many experts (myself included), a custom Application is less "clean" than regular singletons.

Is it correct that the only way to store more or less complex data in Android is to use the built-in SQLite database?

Comparing a singleton to a database is like comparing an apple and an asteroid, on the grounds that both are made of matter and, in English, begin with the letter "a".

A database is persistent. You use a database when you want to save data persistently.

A singleton is not persistent. You use a singleton for transient data, such as a cache of data that is backed by a database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM