Android: onSaveInstanceState in Back button

后端 未结 2 1768
离开以前
离开以前 2021-01-19 18:02

I am developing an application in which i am overriding the back button. I created a check box. On click of which i am calling intent for:

startActivityforR         


        
相关标签:
2条回答
  • 2021-01-19 18:25

    When You start the new Activity then the current Activity gets hidden and new Activity is placed on top of the stack. Now if you press the back button on the new Activity then the first activity should come up in its current state (If it wasn't destroyed, calling finish()) where it was left i.e. if the check box was checked then it should remain checked. You don't need to save the activity state unless the orientation is changed or the activity is destroyed.

    Are you sure you are not doing any thing in the onActivityResult or onResume() method which effects the state of the check box? I would recommend to first comment out all the code in both the methods and see if your check box retains the state. Also can you also make sure that the code itself doesn't uncheck the checkbox before starting the new Activity?

    0 讨论(0)
  • 2021-01-19 18:25

    Now but from second activity if i click on device back button the state is not maintained.

    onSaveInstanceState() is mostly used for configuration changes (e.g., rotating the screen).

    So what should i do to maintain the state on back button

    Most likely, there is no "state" that needs to be "maintained", outside of your data model. Your activity needs to update its data model: files, database, preferences, ContentProvider, some singleton that is your in-memory data model manager, whatever.

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