user2970105
user2970105

Reputation:

RestoreSavedInstance in Android Activity

I am trying to restore an instance of an activity, but it is not passing the variable through eg when I reload the variable it is resetting back to its original value. I know onResume(), onRestorSavedInstanceState and onSavedInstanceState are all running as they are showing in my Logcat. Can anyone explain what I am doing wrong:

private int a;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page2);

        Button btn1 = (Button) findViewById(R.id.button1);      
        btn1.setOnClickListener(listener);  

        if(MainActivity.s1!=null){
        onRestoreInstanceState(MainActivity.s1);
        }
    }



    OnClickListener listener = new OnClickListener() {

        public void onClick(View view) {    
            Intent intent = new Intent(Page2.this, MainActivity.class);
            startActivity(intent);

        }
        };

@Override
protected void onResume(){
    super.onResume();
    a++;
    Log.d("VIVZ", a + "Resume");
}

@Override
        protected void onSaveInstanceState(Bundle outState){
            super.onSaveInstanceState(outState);
            outState.putInt("counter1",  a);
            Log.d("VIVZ", a + " was saved");
            MainActivity.s1=outState;           
        }

@Override
        protected void onRestoreInstanceState(Bundle savedInstanceState){
            super.onRestoreInstanceState(savedInstanceState);
            int a = savedInstanceState.getInt("counter1");
            Log.d("VIVZ", a + " Page 2 was restored");
        }

Upvotes: 0

Views: 116

Answers (1)

Umer Kiani
Umer Kiani

Reputation: 3370

I had this code which i was testing when i was a newbie in android just tested it for you and its working fine.

try this

public class MainActivity extends Activity {

 TextView textviewSavedState;
 EditText edittextEditState;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textviewSavedState = (TextView)findViewById(R.id.savedstate);
     edittextEditState = (EditText)findViewById(R.id.editstate);
    }

 @Override
 protected void onRestoreInstanceState(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onRestoreInstanceState(savedInstanceState);

  String stateSaved = savedInstanceState.getString("saved_state");

  if(stateSaved == null){
   Toast.makeText(MainActivity.this,
     "onRestoreInstanceState:\n" +
     "NO state saved!",
     Toast.LENGTH_LONG).show();
  }else{
   Toast.makeText(MainActivity.this,
     "onRestoreInstanceState:\n" +
     "saved state = " + stateSaved,
     Toast.LENGTH_LONG).show();
   textviewSavedState.setText(stateSaved);
   edittextEditState.setText(stateSaved);
  }

 }

 @Override
 protected void onSaveInstanceState(Bundle outState) {
  // TODO Auto-generated method stub
  super.onSaveInstanceState(outState);

  String stateToSave = edittextEditState.getText().toString();
  outState.putString("saved_state", stateToSave);

  Toast.makeText(MainActivity.this,
    "onSaveInstanceState:\n" +
    "saved_state = " + stateToSave,
    Toast.LENGTH_LONG).show();
 }

}

also refer to this post

Saving Android Activity state using Save Instance State

Upvotes: 2

Related Questions