Search code examples
androidandroid-activitysdkandroid-sdk-toolsactivity-lifecycle

How can i save the desired data on EditText by using onSaveInstanceState() and onRestoreInstanceState() methods


I am trying to design an Android application, in that application i have sent the data from First Activity to Second Activity. In the Second Activity I am using this code

 @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

to save the data when application moves to landscape to portrait mode but in my application even i am using both of these but the data is not being saved. On rotation the data is getting destroyed which application is having in EditText View.

Please Check the below code, give the suggestions where i am doing the mistake to save the data
MainActivity.java

public class MainActivity extends AppCompatActivity {
    EditText name,age;
    TextView text_name,text_age;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name=(EditText) findViewById(R.id.name);
        age=(EditText) findViewById(R.id.age);
        text_name=(TextView) findViewById(R.id.name_edit);
        text_age=(TextView) findViewById(R.id.name_age);
        btn=(Button) findViewById(R.id.click);

        //Button Click to send data to another activity

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();
                String user_name=name.getText().toString();
                String user_age=age.getText().toString();
                bundle.putString("UName",user_name);
                bundle.putString("UAge",user_age);
                Intent intent=new Intent(MainActivity.this,SecondClass.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
    
}

SecondClass.java

public class SecondClass extends Activity {
    EditText name,age;
    TextView text_name,text_age;
    String namer,ager;
    private String savedName,savedAge;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity_layout);
        name=(EditText) findViewById(R.id.name);
        age=(EditText) findViewById(R.id.age);
        text_name=(TextView) findViewById(R.id.name_edit);
        text_age=(TextView) findViewById(R.id.name_age);

        if (savedInstanceState!=null)
        {
            savedInstanceState.get(savedName);
            name.setText(savedName);
            savedInstanceState.get(savedAge);
            age.setText(savedAge);
        }
        if (savedInstanceState==null)
        {
            Intent i=getIntent();
            Bundle bundle=i.getExtras();
            namer=bundle.getString("UName");
            name.setText(namer);
            ager=bundle.getString("UAge");
            age.setText(ager);
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(savedName,namer);
        outState.putString(savedAge,ager);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState!=null)
        {
            savedInstanceState.get(savedName);
            name.setText(savedName);
            savedInstanceState.get(savedAge);
            age.setText(savedAge);
        }
        if (savedInstanceState==null)
        {
            Intent i=getIntent();
            Bundle bundle=i.getExtras();
            namer=bundle.getString("UName");
            name.setText(namer);
            ager=bundle.getString("UAge");
            age.setText(ager);
        }

    }
}


Solution

  • You put and get value in saved instance with a key, and first put your data in 'outState' then call '

    super.onSaveInstanceState(outState);

    try this:

    public class SecondClass extends Activity {
        private static final String SAVED_NAME="savedName";
        private static final String SAVED_AGE="savedAge";
        EditText name,age;
        TextView text_name,text_age;
        String namer,ager;
        private String savedName,savedAge;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.second_activity_layout);
            name=(EditText) findViewById(R.id.name);
            age=(EditText) findViewById(R.id.age);
            text_name=(TextView) findViewById(R.id.name_edit);
            text_age=(TextView) findViewById(R.id.name_age);
    
            if (savedInstanceState!=null)
            {
                namer  = savedInstanceState.get(SAVED_NAME);
                name.setText(namer);
                ager = savedInstanceState.get(SAVED_AGE);
                age.setText(ager);
            }
            if (savedInstanceState==null)
            {
                Intent i=getIntent();
                Bundle bundle=i.getExtras();
                namer=bundle.getString("UName");
                name.setText(namer);
                ager=bundle.getString("UAge");
                age.setText(ager);
            }
    
        }
    
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            outState.putString(savedName,namer);
            outState.putString(savedAge,ager);
            super.onSaveInstanceState(outState);
            
        }
    
        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
            if (savedInstanceState!=null)
            {
                namer  = savedInstanceState.get(SAVED_NAME);
                name.setText(namer);
                ager = savedInstanceState.get(SAVED_AGE);
                age.setText(ager);
            }
            if (savedInstanceState==null)
            {
                Intent i=getIntent();
                Bundle bundle=i.getExtras();
                namer=bundle.getString("UName");
                name.setText(namer);
                ager=bundle.getString("UAge");
                age.setText(ager);
            }
    
        }
    }