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?