Reputation: 2182
The value is entered in edit text in Activity A. When going to another activity B without saving and coming back A , the value of form in edit text in Activity A gets cleared. How can the edit text value gets restored?
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("company", company.getText().toString());
outState.putString("name", name.getText().toString());
outState.putString("email",email.getText().toString());
outState.putString("phone",phone.getText().toString());
outState.putString("address",address.getText().toString());
outState.putString("desc",desc.getText().toString());
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
co = savedInstanceState.getString("comoany");
em = savedInstanceState.getString("email");
ph = savedInstanceState.getString("phone");
add = savedInstanceState.getString("address");
na = savedInstanceState.getString("name");
de = savedInstanceState.getString("desc");
}
@Override
protected void onResume() {
Log.i("this", "Company::::::::::::" + co);
company.setText(co);
super.onResume();
}
Upvotes: 1
Views: 1014
Reputation: 812
what you need is to override onSaveInstanceState in your activity A and store the value of your edit text as follows:
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString(TEXT_VIEW_KEY, mTextView.getText());
// call superclass to save any view hierarchy
super.onSaveInstanceState(outState);
}
and in your onCreate activity should now check if there is any data that was previously saved and then initialize your textview
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// recovering the instance state
if (savedInstanceState != null) {
String text = savedInstanceState.getString(TEXT_VIEW_KEY,);
}
setContentView(R.layout.main_activity);
// initialize member TextView so we can manipulate it later
mTextView = (TextView) findViewById(R.id.text_view);
mTextView.setText(text);
}
You may also want to check on architecture components for android specifically the life cycle aware components to easen your work nxt time
Upvotes: 1
Reputation: 1869
check this question you needn't saving your text in any way just put finish()
in the end of second activity
Upvotes: 1
Reputation: 58934
This is not default behavior of Android.
Activity A will retain values until it is finished (destroyed). Check if you are not calling finish()
after startActivity()
.
Some solutions can be.
EditText
value in SharedPreference.EditText
valueUpvotes: 1