Static class variables reset to null after task kill/restart

Put your problem here if it does not fit any of the other categories.

Static class variables reset to null after task kill/restart

Postby cranke » Tue Apr 12, 2011 2:12 am

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.


-----------------------------
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // File #1: StaticTest.java
  2. // A simple activity that does only two meaningful things:
  3. //   1. It initializes a public static class variable to be non-null ("ourStatic").
  4. //   2. It launches another activity (via an intent).
  5. package com.test.statictest;
  6.  
  7. import java.util.Random;
  8.  
  9. import android.app.Activity;
  10. import android.content.Intent;
  11. import android.os.Bundle;
  12.  
  13. public class StaticTest extends Activity
  14. {
  15.  
  16.     public static Random ourStatic = null;
  17.  
  18.     @Override
  19.     public void onCreate(Bundle savedInstanceState)
  20.     {
  21.       super.onCreate(savedInstanceState);
  22.       setContentView(R.layout.main);
  23.            
  24.       ourStatic = new Random();
  25.      
  26.       Intent intent = new Intent(this, SubActivity.class);
  27.       this.startActivity(intent);      
  28.     }
  29. }
  30.  
Parsed in 0.012 seconds, using GeSHi 1.0.8.4


-----------------------------
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. // File #2: SubActivity.java
  2. // A simple activity that checks the StaticTest.ourStatic class variable for
  3. // nullness.  If null, we write to the log.
  4. package com.test.statictest;
  5.  
  6. import android.app.Activity;
  7. import android.os.Bundle;
  8. import android.util.Log;
  9.  
  10. public class SubActivity extends Activity
  11. {
  12.   @Override
  13.   public void onCreate(Bundle savedInstanceState)
  14.   {
  15.     super.onCreate(savedInstanceState);
  16.     setContentView(R.layout.sublayout);
  17.      
  18.     // Check if our static is still non-null.
  19.     // How can it be null when the only way to get to SubActivity is
  20.     // through StaticTest, and StaticTest initializes
  21.     // StaticTest.ourStatic to be non-null?
  22.     if (StaticTest.ourStatic == null)
  23.     {
  24.       Log.e("OOPS", "Wait, what just happened?  Why are we null?");
  25.     }
  26.   }
  27. }
  28.  
Parsed in 0.013 seconds, using GeSHi 1.0.8.4
cranke
Freshman
Freshman
 
Posts: 6
Joined: Sat Feb 27, 2010 8:36 pm

Top

Re: Static class variables reset to null after task kill/res

Postby cranke » Thu Apr 14, 2011 1:52 am

cranke
Freshman
Freshman
 
Posts: 6
Joined: Sat Feb 27, 2010 8:36 pm

Re: Static class variables reset to null after task kill/res

Postby Charles Elena » Thu Jun 09, 2011 8:20 am

Hey Cranke,

You have given "public static Random ourStatic = null;" then it is obvious that it will reset to null. I think you should keep a flag kind of thing that can be used to reset value. Accordingly you should change value if flag is 1 change the value or if flag is 0 keep value unchanged.

Regards
Charles
Charles Elena
Developer
Developer
 
Posts: 33
Joined: Thu May 26, 2011 11:43 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 8 guests