0

I am using instances of two activies (A and B) among my application. Now I am facing problem of persisting each of them. When I use sharedpreferences, I can get only to persisting A.class and B.class with SharedPreferences, but when I use instance A again. It's persistent state in SharedPreferences is overriden. I think, I should use Bundle wtih onSavedInstanceState and onRestoredInstanceState. But how to pass saved Bundle into onCreate()? Goal is to be able to persist activity instances.

Thanks

Waypoint
  • 17,283
  • 39
  • 116
  • 170

2 Answers2

0

When launching activity A. You can use intent.putExtra( String name, Bundle value ) to pass the bundle to your activity and then in Activity A's onCreate use getIntent.getBundleExtra( String name ) to get your bundle again.

Nicholas Magnussen
  • 769
  • 2
  • 9
  • 26
  • Problem is that activity A is a starting intent, and I don't know how to store Bundle. I am using several instances of one activity, each of them has to be persisted – Waypoint Oct 14 '11 at 10:00
0

You need to consider what state you need to persist.

For example;

  • which checkboxes have been ticked?
  • what level is the user on?
  • what is the position of their cursor

As an example, I just finished an application where a user can view different levels of flashcards.

I care about;

  • which level of flashcard they are looking at (maybe they're at level 2)
  • which card are they looking at (maybe they're on the 4th card)

Here's the code.

// a variable to store the level
private final static String CARD_LEVEL_STATE = "currentCardLevel";

// a variable to store the current card
private final static String CURRENT_CARD_STATE = "currentCardNumber";

@Override
protected void onSaveInstanceState(Bundle outState) {
  // save the level
  outState.putInt(CARD_LEVEL_STATE,   flashCardList.currentLevel());
  // save the current card
  outState.putInt(CURRENT_CARD_STATE, flashCardList.currentCardNumber());
  // do the default stuff
  super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
  // ignore if we have no saved state
  if (savedInstanceState != null) {
    // record the level
    if (savedInstanceState.containsKey(CARD_LEVEL_STATE)) {
      int currentCardLevel = savedInstanceState.getInt(CARD_LEVEL_STATE);
      flashCardList.setCardLevel(currentCardLevel);
    }
    // recover the current card
    if (savedInstanceState.containsKey(CURRENT_CARD_STATE)) {
      int currentCardNumber = savedInstanceState.getInt(CURRENT_CARD_STATE);
      flashCardList.setCurrentCardNumber(currentCardNumber);
    }
    // refresh the view
    refreshCard();
  }
  super.onRestoreInstanceState(savedInstanceState);
};
Matthew Rudy
  • 16,724
  • 3
  • 46
  • 44
  • So what is the result? Am I able to have only onSaveInstanceState and onRestoreInstanceState event though I am using several instances of my acitivities? I want to persist data end GUI state – Waypoint Oct 14 '11 at 09:58
  • When you swap to an activity, Android will call the `onRestoreInstanceState` method on the activity. This is when you should recover state. It is up to android whether it creates a new instance, or swaps to the previous existing instance. You should not try to manually persist an instance. You just need to tell android how to save and load the state you care about. – Matthew Rudy Oct 14 '11 at 10:02
  • OK, but this arise question - how to store Bundle? within method onSaveInstanceState, let's say I save it as a file somewhere, or like a string into SharedPreferences. Application is force closed by system, now I need to restore instance. For this there is a method onRestoreInstaneceState, but it needs Bundle as a parameter. This bundle desn't exist here, it must be first loaded. But where to load it? – Waypoint Oct 14 '11 at 10:12
  • if you look at my example there are 2 parts `onSaveInstanceState` and `onRestoreInstanceState`. They work together to ensure the bundle is saved and restored correctly. Just try it. – Matthew Rudy Oct 14 '11 at 10:17
  • OK, so nevermind the number of instances of activity A, each will have different Bundle when restoring? – Waypoint Oct 14 '11 at 10:18
  • The `Bundle` is `Activity` based. Give it a go. – Matthew Rudy Oct 14 '11 at 10:21
  • I have tried it. I am saving position I have clicked in ListView. Then force close this app. Open again a nothing is persisted. – Waypoint Oct 14 '11 at 10:28
  • ok, well that's not the same thing. This is about temporarily storing activity state, between pause and resume. You are talking about permanent persistence of data. You need to read the documentation on content providers, and sqlite. – Matthew Rudy Oct 14 '11 at 12:47