Global Variable in android app

De openkb
Aller à : Navigation, rechercher

Sommaire

Questions

What is the best way to store data and have access to it from all activities? I ve read that it s possible in two ways:

    • Using a Singleton class
    • Using a class that extends Application class

Is there any known issues regarding both solutions? (flip device, app goes to background, receiving a call)

Answers

I normally use the Singleton approach.

Usually when there should be data that is accessible to everyone, it is reasonable that someone should manage it, so I would create a Singleton class called [Something]Manager that manages that data and allows access to whatever is relevant to other classes.

I haven t encountered any trouble with the things you mentioned: flip device, app goes to background, receiving a call

Source

License : cc by-sa 3.0

http://stackoverflow.com/questions/17468534/global-variable-in-android-app

Related

Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Outils