Passing Extras and screen rotation

后端 未结 1 846
终归单人心
终归单人心 2021-01-26 07:52

This kind of questions appear periodically. Sorry if this has been covered before, but I\'m a newbie and couldn\'t find the appropriate answer. It deals with the correct impleme

1条回答
  •  遥遥无期
    2021-01-26 08:13

    Try this code to store the values for the activity

    Long value;
    
    protected void onSaveInstanceState(Bundle onOrientChange) {
        super.onSaveInstanceState(onOrientChange);
        onOrientChange.putLong("myValue", value);  
    }
    

    And restore the values in onCreate():

    public void onCreate(Bundle onOrientChange) {
      if (onOrientChange!= null){
       value = onOrientChange.getLong("myValue");
      }
    }
    

    Usually you restore your state in onCreate(). It is possible to restore it in onRestoreInstanceState() as well, but not very common. (onRestoreInstanceState() is called after onStart(), whereas onCreate() is called before onStart().

    Use the put methods to store values in onSaveInstanceState()

    0 讨论(0)
提交回复
热议问题