Android HashMap in Bundle?

后端 未结 7 2394
眼角桃花
眼角桃花 2020-12-02 11:57

The android.os.Message uses a Bundle to send with it\'s sendMessage-method. Therefore, is it possible to put a HashMap inside a

7条回答
  •  甜味超标
    2020-12-02 12:25

    Please note: If you are using a AppCompatActivity, you will have to call the protected void onSaveInstanceState(Bundle outState) {} (NOT public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {}) method.

    Example code...

    Store the map:

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSerializable("leftMaxima", leftMaxima);
        outState.putSerializable("rightMaxima", rightMaxima);
    }
    

    And receive it in onCreate:

    if (savedInstanceState != null) {
        leftMaxima = (HashMap) savedInstanceState.getSerializable("leftMaxima");
        rightMaxima = (HashMap) savedInstanceState.getSerializable("rightMaxima");
    }
    

    Sorry if it's some kind of a duplicate answer - maybe someone will find it useful. :)

提交回复
热议问题