Reputation: 3193
I have read through similar questions but have not found the best solution yet. So for example, I load user data from server on application startup. The info is later used in many application components. I do not feel comfortable storing it in singleton (for example, in Application) and make it globally acceptable. The other options I could think of is storing it in shared preferences as JSON or storing it in database. But these approaches might cause some performance hits. How do you solve this common problem?
Upvotes: 3
Views: 67
Reputation: 480
I think you should use Shared Preferences to make the data available throughout you app. Alternativly, you could store your data on internal memory or cache. Depends on the amount of data, depends if you want it to be available between session.
Please read http://developer.android.com/guide/topics/data/data-storage.html for an overview of your options.
Upvotes: 0
Reputation: 154
It seems the best approach is to extend the Application class and add your global variables in, the other Stack Overflow question below could help:
Also, if you have a look to the javadoc of Application class it says clear for global application state use.
Base class for those who need to maintain global application state. You can provide your own implementation by specifying its name in your AndroidManifest.xml's tag, which will cause that class to be instantiated for you when the process for your application/package is created.
See the full Javadoc here
Upvotes: 2
Reputation: 1001
If you want to access data for all the activities, you can try making a base activity that extends activity and then using base activity as your base class for your other activities
public class MainActivity extends BaseActivity
Then for the BaseActivity
public class BaseActivity extends Activity
In the BaseActivity, store all your values as global variables, these variables can then be passed by each activity that extended base activity
Upvotes: 0