0

I have this note taking app where a user can type in what ever he wants to the textbox. But i also want the text to still be there even if the user exits the app. How can i apply onSavedInstantState and RestoreinstantState in the notes.java code?

public class notes extends Activity{

    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notes);

        Button wg = (Button) findViewById(R.id.button3);
        wg.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }

        });



    }
}
Izzy Nakash
  • 177
  • 2
  • 2
  • 12
  • Try this: [http://stackoverflow.com/questions/6525698/how-to-use-onsavedinstancestate-example-please][1] [1]: http://stackoverflow.com/questions/6525698/how-to-use-onsavedinstancestate-example-please – Simon Mar 02 '12 at 17:34

1 Answers1

0

Another way to save state besides onRetainNonConfigurationInstance() (it's deprecated) is onSaveInstanceState

onSaveInstanceState and it's associated methods are hooks that tell you something is happening and you probably want to save/restore. These are usually called configuration changes, but can also happen if the application is closed by android.

You can override these methods much like you did with onCreate:

@Override
public void onSaveInstanceState(Bundle bundle)
{
    //Now you must either save it to memory, the db, a preference, or somwhere else
    bundle.putSerializable("some key", objectToSave);
    super.onSaveInstanceState(bundle);
}

Then you can get that object back in onCreate via the passed in bundle.

If the app exits, you'll have to do more than save it to the bundle. You'll have to persist is somewhere else, and manually load it back when it is needed.

Shellum
  • 3,159
  • 2
  • 21
  • 26
  • For the "objecttosave" what am i supposed to replace it with ? – Izzy Nakash Mar 02 '12 at 20:20
  • Any class you have that is Serializable (implements java.io.Serializable). This class should contain everything that you want to save in memory for configuration changes like screen rotations. – Shellum Mar 02 '12 at 20:50
  • So i just do this? ("some key" Serializable) i dont understand sorry :/ – Izzy Nakash Mar 02 '12 at 21:03
  • Let's say you want to save some strings and a number... create a class... public class MyStuffToSave implements Serializable { String s1; String s1, int i1;} Then assign what you want to save to that class and pass it as the second param in putSerializable. – Shellum Mar 02 '12 at 21:34