0

Hello in my app I need to store single object with several fields. At this moment it is saved like this

@Override
    public Object onRetainNonConfigurationInstance() {
        return UILApplication.advert;
    }

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Advert retainedAdvert = (Advert) getLastNonConfigurationInstance();
        if (retainedAdvert != null) {
            UILApplication.advert = retainedAdvert;
        }
}

Where UILApplication is singleton to keep advert. Sometimes ( often when call camera) advert object is erised to default. So i want to know about save and efficient way to kepp this object. Is it wise to store it in file/ serialise it or create database for a single record or there is something better?

Yarh
  • 4,459
  • 5
  • 45
  • 95

2 Answers2

1

Depends how big your object is. Try with shared preferences: http://developer.android.com/guide/topics/data/data-storage.html#pref

Combination of preferences, right places to populate/read from it, and some static init method could do things for you.

Hope it helps.

1

Taken from my calculator:

@SuppressWarnings("unchecked")
private void loadState() {
    ObjectInputStream ois;
    try {
        ois = new ObjectInputStream(openFileInput(FILE_STATE));
        state = ((State) ois.readObject());
        ois.close();

        ois = new ObjectInputStream(openFileInput(FILE_HISTORY));
        historyListAdapter.setItems((List<String>) ois.readObject());
        ois.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        state = new State();
    } catch (IOException e) {
        e.printStackTrace();
        state = new State();
    } catch (ClassNotFoundException e) {
        Toast t = Toast.makeText(this, "Error parsing saved state",
                Toast.LENGTH_SHORT);
        t.show();
        state = new State();
    }
    setState(state);
}

private void saveState() {
    ObjectOutputStream oos;
    try {
        oos = new ObjectOutputStream(openFileOutput(FILE_STATE,
                Context.MODE_PRIVATE));
        oos.writeObject(state);
        oos.close();

        oos = new ObjectOutputStream(openFileOutput(FILE_HISTORY,
                Context.MODE_PRIVATE));
        oos.writeObject(historyListAdapter.getItems());
        oos.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Override
protected void onPause() {
    saveState();
    super.onPause();
}

Call loadState() in onCreate().

Worked fine for me, I know its not advised to use java serialization in android, but I didn't encounter any errors or bugs whatsoever. No issues with performance either.

You should of course tweak error handling depending on your application.

MattC
  • 12,285
  • 10
  • 54
  • 78
FDIM
  • 1,981
  • 19
  • 21