0

I have an integer that I want to save. I have a class called saving and whenever a method called save() is called, int save increments by one. But I am trying to use onSaveInstanceState, but that won't work and it is never called. How do I save the integer save whenever the saving class is recalled? even on restoresavedinstance state doesn't work as suggested by jerry. Here is my code:

package browser.rarster.com.detailflow;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;

/**
 * Created by RichSharma on 4/28/15.
 */
public class saving extends ActionBarActivity {

    int save = 0;

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("please", save);
        Log.d("gave", "gave it" +  save);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.d("dude", "dude");
    }
    //just call give and give it bundle in giving web and then call savedInsatnce state there

    public void give(){
        save++;
    }

    public int givingWeb(){
        return save;
    }

}
  • do not extend anything sir, just dont, extending what you extending makes it an Activity. and you can not treat an activity like normal java class – Elltz May 15 '15 at 22:18
  • but if i don't extend it, how would i call onSavedInstanceState? is there a way to call it even without extending actionbaractivivty? – user3769474 May 15 '15 at 22:43
  • from how i understand you is you wanted to save an int, that is why i said you dont need to extend ActionbarActivity but if you want to save an int in your Activity then its okay,you can use sharedpreference or the answers might help you, sorry for the confusionm and no sir, you cant call without extending – Elltz May 15 '15 at 22:47

2 Answers2

0

In onRestoreInstanceState(Bundle savedInstanceState) use it like this:

int mySavedInteger = savedInstanceState.getInt("please");

If you want to assign this result to your save variable, use this line of code right after:

save = mySavedInteger;
Jerry
  • 295
  • 3
  • 8
  • Even when i do that, mySavedInteger still seems to reset back to 0. I am not sure why – user3769474 May 15 '15 at 22:18
  • Try not to initialize int save to 0 in the beginning. Just use int save; or use private static int save = 0. Check my edited answer as well. – Jerry May 15 '15 at 22:19
  • even then it doesn't work. i am not sure why, but on saved instance state and on restore instance state isn't getting called. – user3769474 May 15 '15 at 22:29
  • Do you call onCreate() method? – Jerry May 15 '15 at 22:31
  • yeah i did. in onsavedinstancestate i called onCreate(outState); – user3769474 May 15 '15 at 22:41
  • You called onCreate(outState) inside of onSaveInstanceState() method? You have to Override your onCreate method in your class scope, not within another method. – Jerry May 15 '15 at 22:47
  • yeah i did call it inside onSaveInstanceState() method. thank you for your suggestion. – user3769474 May 15 '15 at 22:51
  • how do i override the method in the class scope? – user3769474 May 15 '15 at 22:52
  • Just type in "onCreate", that should give you suggestions and your IDE should override it for you. Then don't forget to use setContentView(R.layout.yourScreen). This is pretty basic stuff, consider reading some basic tutorials, for example this: http://www.tutorialspoint.com/android/android_hello_world_example.htm – Jerry May 15 '15 at 22:55
  • oh yes i actually was calling the wrong onCreate thank you. However, its seems like it is still not saving. its almost like savedInstancestate isn't even getting called. – user3769474 May 15 '15 at 22:58
  • In the worst case, make your save variable static. It's a bit of "hack", but who cares. :) – Jerry May 15 '15 at 23:00
  • 1
    Thank You! It worked! I guess a hack can work once in a while:) – user3769474 May 15 '15 at 23:06
0

To save it :

protected void onSaveInstanceState(Bundle outState) {
  super.onSaveInstanceState(outState);
  outState.putInt("please", save);
}

And restore the value in onCreate() or onRestoreInstanceState():

public void onCreate(Bundle outState) {
  if (outState != null){
    save = outState.getInt("please");
  }
}

You should have a look here for more insights.

Community
  • 1
  • 1
issathink
  • 1,220
  • 1
  • 15
  • 25
  • thanks, but even then, it always, for some unknown reason, goes back to zero. even in oncreate. so i know the problem know is that onsavedinstance state is not getting called. – user3769474 May 15 '15 at 22:33
  • even onCreate, with @Override, isn't getting called? – user3769474 May 15 '15 at 22:36