Using the Android Application class to persist data

how to use two application class in android
android application class lifecycle
android class example
data persistence in android
how to access internal storage android programmatically
base class in android
android application onterminate
android application events

I'm working on a fairly complex Android application that requires a somewhat large amount of data about the application (I'd say a total of about 500KB -- is this large for a mobile device?). From what I can tell, any orientation change in the application (in the activity, to be more precise) causes a complete destruction and recreation of the activity. Based on my findings, the Application class does not have the same life-cycle (i.e. it is, for all intents and purposes, always instantiated). Does it make sense to store the state information inside of the application class and then reference it from the Activity, or is that generally not the "acceptable" method due to memory constraints on mobile devices? I really appreciate any advice on this topic. Thanks!

I don't think 500kb will be that big of a deal.

What you described is exactly how I tackled my problem of losing data in an activity. I created a global singleton in the Application class and was able to access it from the activities I used.

You can pass data around in a Global Singleton if it is going to be used a lot.

public class YourApplication extends Application 
     public SomeDataClass data = new SomeDataClass();

Then call it in any activity by:

YourApplication appState = ((YourApplication)this.getApplication());; // Do whatever you need to with the data here.

I discuss it here in my blog post, under the section "Global Singleton."

Understanding the Android Application Class, To store data using shared preferences, you must first get a SharedPreferences object. There are two Context methods that can be used to� Instead, store any mutable shared data using persistence strategies such as files, SharedPreferences or SQLite. Defining Your Application Class. If we do want a custom application class, we start by creating a new class which extends as follows: Java:

Those who count on Application instance are wrong. At first, it may seem as though the Application exists for as long as the whole app process exists but this is an incorrect assumption.

The OS may kill processes as necessary. All processes are divided into 5 levels of "killability" specified in the doc.

So, for instance, if your app goes in the background due to the user answering to an incoming call, then depending on the state of the RAM, the OS may (or may not) kill your process (destroying the Application instance in the process).

I think a better approach would be to persist your data to internal storage file and then read it when your activity resumes.


I got many negative feedbacks, so it is time to add a clarification. :) Well, initially I realy used a wrong assumption that the state is really important for the app. However if your app is OK that sometimes the state is lost (it could be some images that will be just reread/redownloaded), then it is fully OK to keep it as a member of Application.

How to store data locally in an Android app, As such, use onSaveInstanceState() to store a minimal amount of data storing transient UI state only and use persistent storage for other app data. You should also save the search query itself in the ViewModel object. The SharedPreferences class allows you to save and retrieve key / value pairs of primitive data types. We can use the SharedPreferences to save the primitive data: booleans, floats, ints, longs, and strings. These data will persist in the user session (even if your application is dead).

If you want to access the "Global Singleton" outside of an activity and you don't want to pass the Context through all the involved objects to obtain the singleton, you can just define a static attribute in your application class, which holds the reference to itself. Just initialize the attribute in the onCreate() method.

For example:

public class ApplicationController extends Application {
    private static ApplicationController _appCtrl;

    public static ApplicationController getAppCtrl()
         return _appCtrl;

Because subclasses of Application also can obtain the Resources, you could access them simply when you define a static method, which returns them, like:

public static Resources getAppResources()
    return _appCtrl.getResources();

But be very careful when passing around Context references to avoid memory leaks.

Data and file storage overview, You can specify a custom application class in your Android manifest file. The application Android views store their data via a call to View. If it's general data that pertains to the application as a whole (example: user data), then extend the Application class and store it there. If the data pertains to the Activity, you should use the onSaveInstanceState and onRestoreInstanceState handlers to persist the data on screen rotation.

Dave, what kind of data is it? If it's general data that pertains to the application as a whole (example: user data), then extend the Application class and store it there. If the data pertains to the Activity, you should use the onSaveInstanceState and onRestoreInstanceState handlers to persist the data on screen rotation.

Saving UI States, The user leaves the app using the home button. A few hours later, Android silently kills the app to reclaim some memory. So far, so good! But here� Android allows to persists application data via the file system. For each application the Android system creates a data/data/ [application package] directory. Android supports the following ways of storing data in the local file system: Files - You can create and update files

You can actually override the orientation functionality to make sure that your activity isn't destroyed and recreated. Look here.

Android application and activity life cycle - Tutorial, A singleton responsible for holding the state or session data for the application. A specific task would pull down data from the network, store it,� On Android, there are several solutions to persist data between users’ sessions. One solution is to use a relational database to persist data and then to be able to query easily these data. In standard, Android SDK comes with a SQLite implementation. Note that the following tutorial is also available in video on Youtube :

Don't Store Data in the Application Object, We all know there is an Application class in the Android API, that is usually It is better to store any mutable shared data using persistence� Data used over multiple runs of the application should be persisted like normal in a local database, Shared Preferences, and/or in the cloud. If you want the user to be able to put the app into the

Please, don't use Singletons to persist state on Android, Commonly, you would use a database to store persistent data. and makes it possible for your app to interact with the database using ordinary function calls. How to use a data access object (DAO) to map Kotlin functions to SQL queries. Most of the projects need to persist different data in order to use it in a way or another. So in this tutorial I will show you how to persist your data using Shared Preferences in Android. First you have to know that in Android, if you need to store data you have 4 options to achieve this: 1. Using Shared Preferences 2.

Android Application Class Example, If you have data that's only meaningful for your app, use app-specific storage. For shareable media content, use shared storage so that other apps can access the content. For structured data, use either preferences (for key-value data) or a database (for data that contains more than 2 columns).

  • Just keep in mind that data in your Application can still be deleted if your app goes into the background, so this is not a solution to persisting data you always want to be able to get back. It just serves as a method of not having to recreate expensive objects as often.
  • Mayra; I don't think the app is "usually" deleted (though, as someone points out later in this thread, it "can" be). I'm probably going to go with some type of a "hybrid" approach of using the application to store and load the data, but then use the "android:orientation" attribute on the activity in the manifest file to override the normal behavior of tearing down and rebuilding the activity. All this, of course, assumes that the application can determine "when" it's being destroyed so that the data can be persisted.
  • Unfortunately the blog post in question is no longer available at that address.
  • I've been moving things around on my site. Until it's fixed, you can find it on here:…
  • i know this is an old post but i just came across a problem that this might solve, however this class needs to be declaired in the manifest somehow, no? i cannot acsses the class so feel like this is what i am missing...
  • @ZivKesten How about adding the name= attribute to the application tag inside manifest?
  • @mgc thank you its been a while, and yes that is how i eventually worked it out, also i created instances of that class wherever i needed by giving it the getApplicationContext() with a cast to this class
  • If the Application is killed, then who cares, right? The application is gone. As I understand it, Android will reclaim processes which contain memory like Activities. If the process containing the Application is killed (if Android will even do that?), that is essentially like killing the app. The user will need to launch the app again and, at that point, who cares? It's a new instance of the application.
  • This was an unplesant surprise for us in production. Believe me Android kills processes, it just depends on RAM state and other factors described in the documentation. It was a nightmare for us so I just share my real experience. Well we did not have this on emulators, but in real world some devices are 'overloaded' with apps, so killing a background process is a normal situation. Yes, if user then decides to get the app up into foreground - the OS restores its stack including the Application instance, however there will not be your static data you count on unless you persisted it.
  • I think I'm going to probably use a hybrid approach. I already knew about the manifest trick to override the orientation change (which has other benefits). Since the application is a game, I'm not sure persisting the data between launches is "important" enough; though it probably wouldn't be terribly hard as most of the data can be serialized (though I wouldn't want to serialize and unserialize between every orientation change). I definitely appreciate the input. I wouldn't say those that depend on the App instance are "wrong." A lot depends on the app :).