savedInstanceState returning null

后端 未结 3 1843
长发绾君心
长发绾君心 2021-01-06 18:04

Can someone please explain why the value in my savedInstanceState is null? I have 3 widgets, an EditText, Button and TextView. The person types in what they want. The Phrase

相关标签:
3条回答
  • 2021-01-06 18:15

    You need to use this function onSaveInstanceState(Bundle outState), the one without PersistableBundle outPersistentState

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState, outPersistentState);
        outState.putString("example",newString);
    }
    

    void onSaveInstanceState (Bundle outState, PersistableBundle outPersistentState) this will only get called when you have attribute persistableMode specified in the activity tag inside manifest

    You can read more about it here

    0 讨论(0)
  • 2021-01-06 18:16

    You can define a String variable that is global to your activity and define it upon restoringInstanceState.
    Looks a little something like this:

    String userInput;
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        userInput = savedInstanceState.getString("example") // Refers to your "outState.putString "example" <-- key
        output.setText(newString);
    
    0 讨论(0)
  • 2021-01-06 18:21

    Use below code it works for me.

     @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putString("example",newString);
        }
    
    0 讨论(0)
提交回复
热议问题