Answer the question
In order to leave comments, you need to log in
Why is savedInstanceState always null?
I have two activities. After entering data on the first, the application switches to the second activity. When I click back, I want to see the data entered earlier. But this does not happen - all fields are empty.
public void onSaveInstanceState(Bundle savedInstanceState) is always called when switching between activities. However, onCreate savedInstanceState is always null .
Why is that? And how to fix it?
The code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ButterKnife.bind(this);
if (savedInstanceState != null) {
firstNameEdit.setText(savedInstanceState.getString(STATE_FIRST_NAME_EDITOR));
secondNameEdit.setText(savedInstanceState.getString(STATE_SECOND_NAME_EDITOR));
birthDate.setText(savedInstanceState.getString(STATE_BIRTH_DATE_EDITOR));
}
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString(STATE_FIRST_NAME_EDITOR, firstNameEdit.getText().toString());
savedInstanceState.putString(STATE_SECOND_NAME_EDITOR, secondNameEdit.getText().toString());
savedInstanceState.putString(STATE_BIRTH_DATE_EDITOR, birthDate.getText().toString());
super.onSaveInstanceState(savedInstanceState);
}
Answer the question
In order to leave comments, you need to log in
Decision.
I set the property for the activity in the manifest: android:launchMode="singleTop"
When you open a new activity, do you happen to pull finish() somewhere on the old one? In this case, the data may not be saved.
If you just open a new activity, then the old one will go into the background and when you press up, you will simply return to the old task with all the data. An exception is the situation when the system itself kills your activity (flip, little memory), in this case the system will save the state and upon return a new activity will be created, where this state must be gutted.
Check launchmode in manifest.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question