onSaveInstanceState() onRestoreInstanceState() basic Android example

public class MyClass extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       }
	 String sMyText = "some text";
	 int nMyInt = 10;
 
 @Override
 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.putString("my_text", sMyText);
     outState.putInt("my_int", nMyInt);
     Toast.makeText(this, "onSaveInstanceState()", Toast.LENGTH_LONG).show();
Log.i("onSaveInstanceState", "onSaveInstanceState()");
}
 String sNewMyText = "";
 int nNewMyInt = 0;
 
 @Override
 protected void onRestoreInstanceState(Bundle savedInstanceState) {
     super.onRestoreInstanceState(savedInstanceState);
     // restore saved values
     sNewMyText = savedInstanceState.getString("my_text");
     nNewMyInt = savedInstanceState.getInt("my_int");
     Toast.makeText(this, "onRestoreInstanceState()", Toast.LENGTH_LONG).show();
     Log.i("onRestoreInstanceState", "onRestoreInstanceState()");
 
 }
}
This entry was posted in Android Examples Code and tagged , , . Bookmark the permalink.


Comments are closed.

16,026 views