Hi All,
I'm hoping someone can explain what I am experiencing below ...
I noticed a strange problem with one of my apps. After a task kill
(via, say, a task killer app) and a restart of the app, the public
static class variables seem to get reset to null.
I've created a super simple program to demonstrate this behavior. You
can run it yourself and see the results. Here are the steps:
1. Run the program.
2. Use a task killer to kill the program (is this also the same as
when Android OS decides to kill your program for resource reasons?).
3. Run the program again.
4. Watch the log show "OOPS", "Wait, what just happened? Why are we
null?"
Can someone explain why this happens? And, better yet, a way to work
around it?
Killing and restarting the program some how clears static class
variables. When this happens, the app typically traps because the
statics are null (NullPointerException). Starting the program again
(for a 3rd time) clears everything and all is well until a task killer
again whacks the app.
-----------------------------
// File #1: StaticTest.java
// A simple activity that does only two meaningful things:
// 1. It initializes a public static class variable to be non-null
("ourStatic").
// 2. It launches another activity (via an intent).
package com.test.statictest;
import java.util.Random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class StaticTest extends Activity
{
public static Random ourStatic = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ourStatic = new Random();
Intent intent = new Intent(this, SubActivity.class);
this.startActivity(intent);
}
}
-----------------------------
// File #2: SubActivity.java
// A simple activity that checks the StaticTest.ourStatic class
variable for
// nullness. If null, we write to the log.
package com.test.statictest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class SubActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.sublayout);
// Check if our static is still non-null.
// How can it be null when the only way to get to SubActivity is
// through StaticTest, and StaticTest initializes
// StaticTest.ourStatic to be non-null?
if (StaticTest.ourStatic == null)
{
Log.e("OOPS", "Wait, what just happened? Why are we null?");
}
}
}
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en