Author Topic: Android singleton model for large files  (Read 2234 times)

0 Members and 1 Guest are viewing this topic.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Android singleton model for large files
« on: August 01, 2012, 02:11:09 pm »
Time to post something. And that something will be about android development, since I started it some time ago. More about a work-around that I noticed when I was creating an app that had to use SQLite databases which was preloaded into memory for reading when the app started.

 The database I used was rather large an I had to figure something out when the app was restarted for saving settings by pressing the back key on the phone and launching the app again.

While I was playing around, I noticed that when the back key was pressed, process would not get killed, but the application would close. So when you launch it again, objects would get newly created.

To stop this, I used what is called a “Singleton pattern” in OOP world. Wikipedia describes it as:
Quote
the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system

So because of it, you can only have one and the same object throughout your application. Very useful for settings and such.
 It is very easy to implement in Java, here is an example of a Singleton class:
Code: (java) [Select]
public class SingletonPatternExample {

    private static SingletonPatternExample example = null;
   
    private SingletonPatternExample() {
        // your code like normal goes in here
    }
 
    public static SingletonPatternExample getInstance() {
    if (example == null) {
    example = new SingletonPatternExample();
    }
    return example;
    }
   
    // your code goes below
}

And now in every other class that you want to use it, call the class like this:
Code: (java) [Select]
exFromOtherClass = SingletonPatternExample.getInstance();
So if my application for launched first time, it would take some time to load the database, but when it would be closed and opened again, it would just use the object it created from the last time – this results in a much faster start.

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: Android singleton model for large files
« Reply #1 on: August 01, 2012, 02:22:24 pm »
So is the object still sitting in memory after close?
>>>import this
-----------------------------

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Android singleton model for large files
« Reply #2 on: August 01, 2012, 02:28:49 pm »
So is the object still sitting in memory after close?
Yes, that is the point. Application reloads objects that are created in the activity.

Offline techb

  • Soy Sauce Feeler
  • Global Moderator
  • King
  • *
  • Posts: 2350
  • Cookies: 345
  • Aliens do in fact wear hats.
    • View Profile
    • github
Re: Android singleton model for large files
« Reply #3 on: August 01, 2012, 02:35:49 pm »
Won't the system clear that memory after a while to free up room for other apps?

EDIT:
Found some useful information, if you haven't already seen it.
http://stackoverflow.com/questions/3826905/singletons-vs-application-context-in-android
« Last Edit: August 01, 2012, 02:44:38 pm by techb »
>>>import this
-----------------------------

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: Android singleton model for large files
« Reply #4 on: August 01, 2012, 02:58:49 pm »
I dunno the inner-workings of DalvikVM, but I would assume that if you keep the app closed for a long time, the system would kill the process sooner or later. It's how Android works anyway...