4

I am developing an application in which i am overriding the back button. I created a check box. On click of which i am calling intent for:

startActivityforResult();

And also maintaining the state of activity as :

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);
  savedInstanceState.putBoolean("checkbox", checkbox.isChecked());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  checkbox_state = savedInstanceState.getBoolean("checkbox");
}

which run fine and the state is maintained. Means i am entering value in edit text. and on check box click calling new activity for result and while return on first activity the state is maintain.

Now but from second activity if i click on device back button the state is not maintained.

So what should i do to maintain the state on back button. I searched but not found satisfied solution. Please suggest me.

user_CC
  • 4,686
  • 3
  • 20
  • 15
Manoj Fegde
  • 4,786
  • 15
  • 50
  • 95

2 Answers2

1

Now but from second activity if i click on device back button the state is not maintained.

onSaveInstanceState() is mostly used for configuration changes (e.g., rotating the screen).

So what should i do to maintain the state on back button

Most likely, there is no "state" that needs to be "maintained", outside of your data model. Your activity needs to update its data model: files, database, preferences, ContentProvider, some singleton that is your in-memory data model manager, whatever.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Lets say I have a ListView which I populate from the database. Do you reckon that I should populate it from database every time when I navigate back to it from another activity? Or I should keep the adapter content saved in state and reuse it? Just like to know your take on it :) – Ε Г И І И О May 02 '13 at 15:16
  • 1
    @ΕГИІИО: Populating from the database is safest, as the data may well have changed since the activity was last around. Saving it in state is the worst choice. Middle ground would be to maintain some sort of data model management singleton that caches information and is the point of access to the data model by *all* activities. – CommonsWare May 02 '13 at 15:24
  • Is it the worst because it wastes resources? If you know the data is not getting updated while you navigate between those two activities, wouldn't it be more responsive to restore it from state? Because I hate to see 'loading please wait...' stuff in between my navigations or when i rotate the screen :) – Ε Г И І И О May 02 '13 at 15:37
  • @ΕГИІИО: "Is it the worst because it wastes resources?" -- if you are going to take up gobs of RAM, do so in a way to take better advantage of it. Also, it is the most likely to be out of date. "wouldn't it be more responsive to restore it from state?" -- yes, if you ignore all other concerns. "Because I hate to see 'loading please wait...' stuff in between my navigations or when i rotate the screen" -- that you have other problems, unless Traceview shows that 100% of your delay is in the database I/O. – CommonsWare May 02 '13 at 15:51
  • @ΕГИІИО: For configuration changes, a better solution is to retain the `ListFragment`. Navigations should not cause a database re-query unless the previous activity instance had been `finish()`'d. – CommonsWare May 02 '13 at 15:53
  • Thank you very much for your valuable advice. I will look into ListFragment implementation. And I should see what Traceview is :) Damn I'm so new to Android! Thanks again. – Ε Г И І И О May 02 '13 at 15:59
1

When You start the new Activity then the current Activity gets hidden and new Activity is placed on top of the stack. Now if you press the back button on the new Activity then the first activity should come up in its current state (If it wasn't destroyed, calling finish()) where it was left i.e. if the check box was checked then it should remain checked. You don't need to save the activity state unless the orientation is changed or the activity is destroyed.

Are you sure you are not doing any thing in the onActivityResult or onResume() method which effects the state of the check box? I would recommend to first comment out all the code in both the methods and see if your check box retains the state. Also can you also make sure that the code itself doesn't uncheck the checkbox before starting the new Activity?

user_CC
  • 4,686
  • 3
  • 20
  • 15