Reputation: 6263
So I have this Java framework I made a while back while working with Swing apps. It's one of those things that was more of an academic exercise of "can I really do this?" than something that I've used a whole lot. Basically, it works like this:
Reads an XML configuration file at startup with information, including package paths.
Scans those packages for annotated classes, like Spring. The annotated classes are resources in the traditional MVC style (ie, models, views, controllers).
Stores references to class types and methods that match in a single, global storage class.
Provides static methods from a central class, which allows the various MVC components to communicate with each other through reflection, thus allowing for maximum compartmentalization of application components, since none of them have direct references to each other.
I would really like to use this with Android, but I know I would have to make a few changes to it. I've read that package scanning can't be done well on Android, but I've figured out a way around that. My main concern is with reading the XML configuration file and building the global storage class.
In a traditional Java app, this is done at startup of the application, and the data is preserved in memory until the app is shutdown. When it is next started up, the configuration is read again, the scanning is done again, etc. Android, however, has a different lifecycle, with the application being killed/restarted at various times based on memory needs.
QUESTIONS
How does the Android memory model work for in-memory data when an app is a) put to "sleep", and b) killed by the system to free up space? For the latter condition, is information preserved in some way?
Based on the prior question, how would I ensure that the configuration file is read and the storage setup properly throughout the lifecycle of the application? I believe the answer is in the Android Application class, probably by tying the framework to its lifecycle, but I'm looking for guidance there.
My background is heavily in pure Java, I'm still relatively new to Android overall.
Thanks in advance.
Upvotes: 0
Views: 143
Reputation: 1006614
How does the Android memory model work for in-memory data when an app is a) put to "sleep"
I do not know what "app is put to sleep" means, as that term is not used in conventional Android development. Devices go into a sleep mode; that has no direct impact on the process lifecycle of apps.
How does the Android memory model work for in-memory data when... b) killed by the system to free up space?
Apps are not killed. Processes are terminated. As with process models on most major operating systems, when the process is terminated, all of its consumed RAM returns to the system.
For the latter condition, is information preserved in some way?
Only if you preserve it yourself, by persisting data somewhere. Again, this is not significantly different than with the process model used by other major operating systems. What is different is that the system is in charge of the timing of process termination.
how would I ensure that the configuration file is read and the storage setup properly throughout the lifecycle of the application?
That is difficult to answer in the abstract. onCreate()
of your Application
subclass will be called when the process is created. You are welcome to do some initialization work there, bearing in mind that every millisecond you spend here is a millisecond that the user is waiting for your app to start. Also bear in mind that onCreate()
of Application
will be called for any reason the process is starting up, including things that have nothing to do with your UI (e.g., responding to a system broadcast).
Upvotes: 2