Android: Saving and Restoring data when switching between Activities

后端 未结 1 1274
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-20 04:18

I am trying to save some data in \"MainActivity\" when switching to another activity, and restoring that data as I switch back to it.

In \"MainActivity\": (restoring

相关标签:
1条回答
  • 2020-12-20 04:33

    You can use onRestoreInstanceState() witch is called after onStart(), whereas onCreate() is called before onStart().

    Use the put methods to store values in onSaveInstanceState():

    protected void onSaveInstanceState(Bundle bundle) {
      super.onSaveInstanceState(bundle);
      bundle.putInt("value", value);
    }
    

    And restore the value in onCreate():

    public void onCreate(Bundle bundle) {
      if (bundle!= null){
        value = bundle.getInt("value");
      }
    }
    
    0 讨论(0)
提交回复
热议问题