0
votes

I currently have a mobile application on the playbook that has the following class:

[Bindable]
public class Foo
{
    public var myString:String;
    public var myList:ArrayCollection;
    public function Foo() {} 
}

I also have persistNavigatorState="true" in my ViewNavigatorApplication.

Suppose in my first view I have the following in my creationComplete="init()" call:

private function init():void {
    var s:String = "test_string";
    var a:ArrayCollection = new ArrayCollection();
    a.addItem("test1");
    a.addItem("test2");
    a.addItem("test3");

    data.foo = new Foo();
    data.foo.myString = s;
    data.foo.myList = a;

    trace(data.foo.myString);
    trace(data.foo.myList[0]);
    trace(data.foo.myList[1]);
    trace(data.foo.myList[2]);
}

When executed, everything works fine in my app. However, since I want the sessions to persist in case the user accidentally closes the app, when he re-opens it the data should still be there.

Instead, when I close and re-open my app only the myString property persists (ie traces "test_string", as intended), however the ArrayCollection isn't copied.

I've tried the following with ObjectUtil.clone() and ObjectUtil.copy():

data.foo.myString = ObjectUtil.copy(s) as String;
data.foo.myList = ObjectUtil.copy(a) as ArrayCollection;

and I've also tried:

var f:Foo = new Foo();
f.myString = s;
f.myList = a;
data.foo = ObjectUtil.copy(f) as Foo;
trace(data.foo.myString);
trace(data.foo.myList[0]);

but this only throws me a

TypeError: Error #1009: Cannot access a property or method of a null object reference.

Any ideas on how to persist ArrayCollections and Foo class in a mobile application?

1

1 Answers

0
votes

I'm not 100% sure, but I was wondering about this type of problem while working on a mobile app recently.

I believe your problem might be happening b/c your are setting the data manually on the View, instead of passing it into the View with the ViewNavigator.pushView() method.

I just browsed through the source, and it looks like setting the data directly on the View will bypass ViewNavigator's data persistence. Though with that said, I'm not sure why it would even remember the value for that String :)

I would try to do the following:

  • don't set the View's data property from inside the view, as you are doing now in the creationComplete handler
  • if possible, use the "firstView" property of ViewNavigatorApplication in mxml
  • if possible, initialize the "firstViewData" property in mxml (may not be possible)
  • if you can't do the two above, in your application's startup code call navigator.pushView(View_Class_Name, foo) to pass in the data.