The Mobile Experiment IV – I wasn’t persistent enough

After going through the previous code, from the previous post, I realized that something was missing. What if, after starting a task (pressing the Start button), the phone is turned off for any reason such as  low battery, flushed away in the washroom, etc…) ?

Well, if you flush your phone away with your other “things”, the application doesn’t matter anymore, but all other reasons actually happen all the time. The way my code was done, you’d gonna loose the current state, and when restarted it would display empty fields and the Start button again, while the right behavior is show you where you left it before the restart.

To solve this problem, I realized that I also must save the state and content of all UI elements when starting a new task. Since the state and content aren’t the final data that will be save into the database, I believe the right solution is also using the PersistentStore and PersistentObject classes. That said, using the Vector as my PersistentObject doesn’t suit me anymore.

My new persistent object should be able to keep:

  • The Vector for my projects list;
  • The task current state (when already started);
  • The Notes Field content;
  • The start Date and Time;

Since I’m dealing with different types of data, I decided to create a class to hold them all:

public class PersistentTimeTracker implements Persistable {
    Vector projectList;	// Current project state
    Long startDateTime;	String noteTask;
    String selectedProjectItem;	int buttonState;
    public PersistentTimeTracker() {
           projectList = new Vector(10);
           buttonState = 0;
           noteTask = "";
           selectedProjectItem = "";
           startDateTime = new Long(0L);

The class that is going to be used as PersistentObject must implement the Persistable interface, and all its variables must be persistable as well. Other than that, the code is pretty much the same as before. Again, the beauty here is the fact that I need to instantiate the class only on the first run. After that, the object is retrieved from the PersistentStore:

// Get the content, if it exists

if (persistentProjectList.getContents() == null) {
    // retrieve object list
    projectState = new PersistentTimeTracker();
} else {
    projectState = (PersistentTimeTracker)persistentProjectList.getContents();

The projectState object is then used to fill in the UI elements:


To save the persistent data, it is necessary just to get the values from the UI elements setting the correspondent field in the PersistentTimeTracker, and finally commit it.

private void savePersistentData()	{
    // check if the selected string is already in the array
    String newProject = projectListField.getEditField().toString();
    projectState.noteTask = notesField.getText();
    projectState.startDateTime = new Long(dateField.getDate());
    projectState.selectedProjectItem = projectListField.getEditField().getText();
    if (projectState.projectList.contains(newProject) == false) {

Next time, the idea is to save the task in the final database. I’m almost finished with this one!

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: