2

i have an app that has three pages, one of them is the main page. the user can enter in a few fields that i would like to save if the user goes to one of the two sub pages. i have been looking into the onPause() and into onSaveInstanceState(). i guess i just want a clear explanation on the two, and if onPause() is better and example of the code. this is what i ahve for onSaveInstanceState().

protected void onSaveInstanceState(Bundle outState) {
    // Save away the original text, so we still have it if the activity
    // needs to be killed while paused.

    outState.putDouble("quizPts",qpts);
    outState.putDouble("quizV",qvalue);
    outState.putDouble("tPts",tpts);
    outState.putDouble("tValue", tvalue);
    outState.putDouble("hPts", hpts);

so that is how i am setting up bundle, by giving it an ID and a value.

public void onRestoreInstanceState(Bundle outState) {
  super.onRestoreInstanceState(outState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  qpts = outState.getDouble("quizPts");
  qvalue = outState.getDouble("quizV");
  tpts = outState.getDouble("tPts");
  tvalue = outState.getDouble("tValue");
  hpts = outState.getDouble("hPts");

this is how i plan to retore it, the problem is i dont understand how to pass the Bundle around to restore it. i am setting the variables that i need to back to the variables that get set to the UI.

any advice would be great

thanks from a beginner androider

zach
  • 1,281
  • 7
  • 27
  • 41

2 Answers2

1

The best option will be a shared preference. The onpause is designed for attending your concerns when app is paused due to a phone call or something. But if you use shared preference it gives you the method for saving your data and recover it wit default values if the saved value is not available. This data will persist across user sessions (even if your application is killed). But it is not a good option if you are planning to save something other than primitive data types like bool,int etc.

see http://developer.android.com/guide/topics/data/data-storage.html#pref

scari
  • 86
  • 2
  • 10
0

You don't need to pass the bundle around yourself: the Activity framework takes care of that. Use onSaveInstanceState(): if your Activity class is destroyed for any reason by the system it will be called so you should be fine putting your logic in there. onPause will always be called if you leave your Activity, regardless of if the Activity is destroyed.

I would also add a check in your onRestoreInstanceState:

public void onRestoreInstanceState(Bundle outState) {
  super.onRestoreInstanceState(outState);
  // Restore UI state from the savedInstanceState.
  // This bundle has also been passed to onCreate.
  if(outState.containsKey("quizPts")) qpts = outState.getDouble("quizPts");
  if(outState.containsKey("quizV")) qvalue = outState.getDouble("quizV");
  if(outState.containsKey("tPts")) tpts = outState.getDouble("tPts");
  if(outState.containsKey("tValue")) tvalue = outState.getDouble("tValue");
  if(outState.containsKey("hPts")) hpts = outState.getDouble("hPts");
Femi
  • 64,273
  • 8
  • 118
  • 148
  • so if i dont pass anything do i need the Bundle to be global? if i do onSaveInstanceState() is that being redundant? do i need to do both or just pick one? – zach Jun 28 '11 at 15:22
  • The Bundle is provided for you, the same way the method is called by the Android platform. You don't need to do anything at all. No global variables, nothing at all. Just implement both methods and they will be called when needed. – Femi Jun 28 '11 at 16:06
  • Re "if your Activity class is destroyed for any reason by the system it will be called". Not for *any* reason, but in OP's use case, yes. Can't rely on onSaveInstanceState for any truly important information. – ToolmakerSteve Sep 26 '15 at 16:56