1

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?

1

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.