savedInstanceState is always null

前端 未结 13 2099
星月不相逢
星月不相逢 2020-11-29 00:53

This is my savedInstaceState code:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) 
{
    savedInstanceState.putStringArrayList(\"todo_a         


        
相关标签:
13条回答
  • 2020-11-29 01:36

    Did you check if you have an Id set for that view ( if a view it is/has...). onSaveInstanceState() is not called otherwise.

    Check this link.

    0 讨论(0)
  • 2020-11-29 01:36

    Implement a method of onRestoreInstanceState and put below code there

    Altodo = savedInstanceState.getStringArrayList("todo_arraylist");
    
    0 讨论(0)
  • 2020-11-29 01:37

    Check your activity in AndroidManifest.xml and remove android:noHistory property if is true.

    <activity
        // ....
        android:noHistory="false" />
    
    0 讨论(0)
  • 2020-11-29 01:41

    How do you test it?

    Imo the best way to test it is using the "Don't keep activities"-flag in Settings > Developer Options. If you don't have Developer Options in Settings, see Enabling On-device Developer Options.

    1. Open your activity
    2. Long-press home
    3. Go to another application
    4. Long-press home
    5. Go back to your application
    0 讨论(0)
  • 2020-11-29 01:45

    I found that when I override onSaveInstanceState() and actually save some data in the Bundle, instance state is restored. Otherwise it's not.

    0 讨论(0)
  • 2020-11-29 01:47

    The state saved in this manner is not persisted. If the whole application is killed as you are doing during debugging, the bundle will always be null in onCreate.

    This IMO is yet another example of awful Android documentation. It's also why most apps in the marketplace don't implement saving state properly (at all).

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