Joakim
Joakim

Reputation: 3294

Objects implicitly owned by Application object is garbage collected when app is in background

I have a situation where my Android application crashes when it has been in the background for a while. To me it seems like it's because some of my objects are garbage collected. My structure and problem is the following

Object A is owned by the Android Application Object and created in the onCreate() method.

public class Application extends android.app.application {

    private static Application instance;
    private A a;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        a = new A(getApplicationContext());
    }

    public static A getA() {
        return instance.a;
    }
}

An ArrayList is created in the constructor of object A.

The ArrayList is populated with objects of type B via an async call (Downloaded from a server).

Now when my application have been in the background for a while (usually over night) and I resume it, it crashes because the ArrayList is empty. (ArrayList is never empty during normal use and definitely wasn't before I sent the app to the background (last night).

So I'm guessing that my B objects are garbage collected and I really don't understand how this can happen, since they are implicitly owned by the Android Application Object.

Does anyone hve any input on this?

Upvotes: 0

Views: 56

Answers (1)

JoeyJubb
JoeyJubb

Reputation: 2321

You should probably override the onTrimMemory() and onLowMemory() callbacks in the Application class -- at least these will give you warning that you're about to lose your data

Upvotes: 1

Related Questions