Recreating activity state after clicking back button

前端 未结 1 319
借酒劲吻你
借酒劲吻你 2021-01-24 04:11

I can\'t find out why savedInstanceState is always null in the onCreate method when I\'m successfully saving data to the Bundle in onSaveInstanceState method. When I\'m running

相关标签:
1条回答
  • 2021-01-24 05:02

    That is expected. When you press back, the Activity is destroyed and the data it had is lost. After this you will always get null in the bundle as a new, fresh instance is being created when you reopen the app.

    The Bundle savedInstanceState is used when the activity is being recreated due to some changes (like rotation), or because it was paused in the background for a long time.

    If you want to persist some data, consider SharedPreferences for small stuff, or maybe a database (SQLite, Realm) or files for large stuff.

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