Q:

Blackberry: saving ListField content and dirty state management

I have prepared a simple test case to demonstrate my problem. 
It is just 1 file which will run instantly when added to a new project.
I would like to have a MainScreen displaying an editable list of items:

and when leaving this screen, the user should be asked - if she wants to save the modified list to persistent storage, by presenting the standard Save/Discard/Cancel-dialog:

I have added setDirty(true) to my menu items and the standard dialog does come up okay.
My problem is: I don't know how to clear the dirty flag after saving - in my current code the Save/Discard/Cancel-dialog comes again and again, even if I just view the ListField, without editing it.
src\mypackage\MyList.java:
package mypackage;

import java.util.*;
import net.rim.device.api.collection.*;
import net.rim.device.api.collection.util.*; 
import net.rim.device.api.system.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.decor.*;
import net.rim.device.api.util.*;

public class MyList extends UiApplication implements FieldChangeListener {
    MyScreen myScreen = new MyScreen();

    public static void main(String args[]) {
        MyList app = new MyList();
        app.enterEventDispatcher();
    }

    public MyList() {
        MainScreen titleScreen = new MainScreen();
        titleScreen.setTitle("Click the button:");

        ButtonField myButton = new ButtonField("Show the list", ButtonField.CONSUME_CLICK) ;
        myButton.setChangeListener(this);
        titleScreen.add(myButton);

        pushScreen(titleScreen);
    }

    public void fieldChanged(Field field, int context) {
        pushScreen(myScreen);
    }
} 

class MyScreen extends MainScreen {
    ObjectListField myList = new ObjectListField();
    static PersistentObject myStore;
    static Vector myData;

    static {
        myStore = PersistentStore.getPersistentObject(0xb77f8e453754f37aL);
        myData = (Vector) myStore.getContents();
        if (myData == null) {
                myData = new Vector();
                myData.addElement("String 1");
                myData.addElement("String 2");
                myData.addElement("String 3");
                myStore.setContents(myData);
        }
    }

    public MyScreen() {
        setTitle("Edit the list below:");

        add(myList);

        addMenuItem(addItem);
        addMenuItem(editItem);
        addMenuItem(removeItem);
    }

    // load data from persistent store into the ListField
    private void loadData() {
        // clear the ListField
        myList.setSize(0);
        // copy data from the Vector to the ListField
        for (int i = myData.size() - 1; i >= 0; i--)
            myList.insert(0, myData.elementAt(i));
    }

    // save data from the ListField into the persistent store
    private void saveData() {
        // clear the Vector
        myData.removeAllElements();
        // copy data from the ListField to the Vector
        for (int i = myList.getSize() - 1; i >=0; i--)
            myData.addElement(myList.get(myList, i));

        synchronized(PersistentStore.getSynchObject()) {
            myStore.commit();
        }
    }

    protected void onUiEngineAttached(boolean attached) {
        if (attached) {
            loadData();
        }
    }

    public void save() {
        saveData();
        // UPDATE: when I call setDirty(false); here, then
        // the app starts displaying Save/Discard/Cancel dialog
        // on its exit - so there must be a better way...
    }

    private final MenuItem addItem = new MenuItem("Add Item", 0, 0) {  
        public void run() { 
            String[] buttons = {"Add", "Cancel"};
            Dialog myDialog = new Dialog("Add Item", buttons, null, 0, null);

            EditField myEdit = new EditField("Item: ", "");
            myDialog.add(myEdit);

            if (myDialog.doModal() == 0) {
                myList.insert(0, myEdit.getText());
                setDirty(true);
            }
        }
    };

    private final MenuItem editItem = new MenuItem("Edit Item", 0, 0) {  
        public void run() { 
            String[] buttons = {"Save", "Cancel"};
            Dialog myDialog = new Dialog("Edit Item", buttons, null, 0, null);

            int index = myList.getSelectedIndex();
            if (index == -1) {
                return;
            }

            String selectedItem = (String) myList.get(myList, index);
            EditField myEdit = new EditField("Item: ", selectedItem);
            myDialog.add(myEdit);

            if (myDialog.doModal() == 0) {
                myList.set(index, myEdit.getText());
                setDirty(true);
            }
        }
    };

    private final MenuItem removeItem = new MenuItem("Remove Item", 0, 0) {  
        public void run() { 
            String[] buttons = {"Delete", "Cancel"};
            Dialog myDialog = new Dialog("Remove Item", buttons, null, 0, null);

            int index = myList.getSelectedIndex();
            if (index == -1) {
                return;
            }

            String selectedItem = (String) myList.get(myList, index);
            LabelField myLabel = new LabelField("Really delete " + selectedItem + "?");
            myDialog.add(myLabel);

            if (myDialog.doModal() == 0) {
                myList.delete(index);
                setDirty(true);
            }
        }
    };
}

Please share your Blackberry 6 experience, advices in regard to persistent storage are also welcome.
In my real program I'm using KeywordFilterField for viewing a SortedReadableList, so from reading Blackberry docs I suppose, that I must always copy data between SortedReadableList and Vector - because the latter is persistable and the former is not?

A:

setDirty(false) will clear the dirty flag if that is what you are after.

