0

In my Android activitity, I am using two different arrays. First, I am declaring them, and then in the onCreate() method, I am instantiating them. However, when I populate them and then change the orientation, they are getting instantiated again in the and the data is lost.

public class MainActivity extends Activity {

    private JSONArray first;
    private JSONArray second;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        interestedSections = new JSONArray();
        productsBought = new JSONArray();
    }

        //...
}

I tried to add if (savedInstanceState != null) before initializing the arrays, so as to initialize them for the first time only, however when I change the orientation, they are being null. What can I do in order to persist the data in the arrays throughout the whole application lifecycle please?

duncanportelli
  • 3,161
  • 8
  • 38
  • 59
  • Do you mean application life cycle or activity life cycle? I ask because if they are nulled when you rotate, then they are declared in your activity and therefore are in scope to that activity only. – Simon Apr 18 '13 at 05:42

3 Answers3

1

Check out this answer as onCreate is called when the screen is rotated: Activity restart on rotation Android

Edit: If you want a quick and dirty way to make it work, just have a static initialized boolean and set it to true onCreate. Don't set the arrays to new arrays if initialized is true.

Community
  • 1
  • 1
Andrew T.
  • 4,598
  • 4
  • 35
  • 54
  • Hacks and workarounds! An extra variable just to tell you if something has been initialised? Whatever happened to `if(thing==null)`? – Simon Apr 18 '13 at 05:38
  • @Simon this is just theoretical, but what if the variable could be set to null during the ("first") initialization? You really would need a static variable to check for initialization, sadly. The best way to fix this would be to follow the link and add the "onconfigchanges" bit to EVERY activity in your program, since I assume you didn't want it to do that to begin with. Silly Android and its horrible default behavior. –  Apr 30 '13 at 21:19
0

I had to declare the arrays as static and in the onCreate() method, I initialize them only for the first time:

public class MainActivity extends Activity {

private static JSONArray first;
private static JSONArray second;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if (savedInstanceState == null) {
        first = new JSONArray();
        second = new JSONArray();
    }
}

    //...

}

duncanportelli
  • 3,161
  • 8
  • 38
  • 59
  • Glad you fixed it, but they don't need to be static and if you pass the values anywhere else, definitely should not be. I'm really confused though. You declare `first` and `second` and the initiailse `interestedSections` and `productsBought`. Where do you intialise first and second? – Simon Apr 18 '13 at 05:53
  • if I don't declare them as static, when I change the orientation, they will be `null`. – duncanportelli Apr 18 '13 at 10:13
  • If you can wait a few hours (at work now) I'll respond fully. You might have introduced a big memory leak. I think you are not understanding what's going on fully. – Simon Apr 18 '13 at 13:44
  • But the memory leak happens if the array is not emptied at all, no? I am clearing those 2 arrays everytime the user clicks a particular button – duncanportelli Apr 19 '13 at 06:34
0

See onSaveInstanceState(), this is where you are supposed to save your arrays to the bundle.

By the way, Android sometime can kill the process and restore the activities later; the static variables will not survive this.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127