1
public class Checker {
    static private int value1 = 0, value2 = 1;

    static private Activity activity;

    public static void init(Activity activity) {
        Checker.activity = activity;

        value1 = // calculate value

        value2 = // calculate value, normally the same as value1
    }

    public static void check() {
        if (value1 != value2)
            activity.finish();
    }
}

I got the above class and placed a call to Checker.init() in onCreate of the main activity and calls to Checker.check() at various points of my code. Problem is that I get a NullPointerException in Checker.check() for activity.finish() when re-opening the app after it has been backgrounded for some time.

Normally value1 == value2, so this exception seems to indicate that all the static variables were reset to default. I am a Java/Android newbie, but I thought these variables are retained as long as the activity is in memory and in case the activity is killed onCreate and so Checker.init() called again. So why does this happen?

Ezekeel
  • 13
  • 4
  • Why don't you just change the signature of `check` to pass in the `Activity`, i.e., `public static void check(Activity activity)`? Presumably you are calling `check` in the Activity you want to finish. – Brian Cooley Mar 29 '12 at 16:45
  • Because that does not solve the problem. Normally this line should not even be executed because value1 == value2. It only gets executed due to the apparent reset of the static variables so that value1 != value2. – Ezekeel Mar 29 '12 at 16:54

2 Answers2

2

Based on the symptoms you are describing, what's most likely happening is that the ClassLoader for Checker is getting garbage collected but your Activity is not being garbage collected. So when the app returns from the background, Activity goes to onResume. When the call to Checker.check() is made, the Checker class is reloaded with the default values of value1 and value2.

One way to make sure that the Checker class stays around as long as the Activity is to keep a reference to a Checker instance in the Activity class. Of course, you might also consider redesigning the Checker functionality so that it doesn't depend on static member behavior.

Brian Cooley
  • 11,622
  • 4
  • 40
  • 39
  • I already tried putting Checker.init() in onResume of the main activity, but that did not solve the issue. I will see if initializing a Checker instance in my main activity will prevent the class from being GC'd. – Ezekeel Mar 29 '12 at 18:14
0

This is most likely caused by the activity not existing any more. If its in the background the os might kill it and restart it when needed, so you will need to check if it is != null before you try and finish() it :)

if (value1 != value2 && activity != null) activity.finish();

Carl-Emil Kjellstrand
  • 1,233
  • 1
  • 10
  • 17
  • According to http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle, in case the activity is killed onCreate and so Checker.init() called again. – Ezekeel Mar 29 '12 at 16:58