Kiotto
Kiotto

Reputation: 315

ANDROID - class Application

I'm trying to understand how the Application class.

I've noticed that need to declare it in <application> manifest within the tag and then can access the variables in other classes as they were global variables. And even out of the application the value of these varieties do not change.

However, if you unplug the phone, the next time you turn it on and start applying the value of the variables returned to its initial state. I wonder if you can maintain the state of variables when we turned off the phone and reconnect it?

Upvotes: 1

Views: 217

Answers (2)

Rotemmiz
Rotemmiz

Reputation: 7983

I'm not sure I fully understand what you mean, but it seems like you want to use Shared Preferences.

try this Question: Android - How Do I Set A Preference In Code

Upvotes: 0

Guillaume
Guillaume

Reputation: 22822

Application data is available as long as your application is "active". When the OS decides to terminate it to clear memory, so goes your application data (you typically don't control when this happens, as per the mobile development good practices: the OS decides on its own), and it's not persisted for the next time you start the app. So anything you store in the Application should be stored again each time the app is started.

It should be used to keep short-term data available to you. A good use case is when you need to access a complex data structure from multiple activities: it's not possible to use bundles for that. You can generate your complex data structure in your start activity, store it in the application, and then retrieve it in any other application that may need it.

But you should not use it for long-term persistent data. For that, the best is to use a SQLite database.

Upvotes: 2

Related Questions