I have this following code:
public class MainActivity extends AppCompatActivity {
EditText edtInput;
private static final String STATE_RESULT = "state_result";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edtInput = (EditText) findViewById(R.id.edtInput);
}
@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
outState.putString(STATE_RESULT, edtInput.getText().toString());
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null){
edtInput.setText(savedInstanceState.get(STATE_RESULT).toString());
}
}
}
On Pixel 3 (running Android 12): type something in the EditText, then do a combo of screen rotations, turning on/off the screen, press back to home and open the app again. the user input is still on EditText
On Xiaomi Pocophone F1 (running Android 10) there's a slightly different behaviour: EditText is cleared if you press back and open the app again. but no problem as long as you only turn on/off or rotate the screen.
Not sure if I misunderstood Android activity lifecycle, or this is a bug on the OS. Any help is appreciated.