2

I need to store in a Bundle two SparseArray.

So here it is:

    protected void onSaveInstanceState(Bundle state) {
        super.onSaveInstanceState(state);

        state.putSparseParcelableArray("guybrushInsulti", (SparseArray<String>) gameEngine.getDialogs().getInsultKnow());
        state.putSparseParcelableArray("guybrushControInsulti",(SparseArray<String>) gameEngine.getDialogs().getControInsultKnow());
        state.putSerializable("level", gameEngine.getWorld().getLevel());   
}

Eclipse says:

The method putSparseParcelableArray(String, SparseArray<? extends Parcelable>) in the type Bundle is not applicable for the arguments (String, SparseArray<String>)

OnRestoreInstanceState:

 @Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    SparseArray<String> mInsultiKnow = savedInstanceState.getSparseParcelableArray("guybrushInsulti");
    SparseArray<String> mControInsultiKnow = savedInstanceState.getSparseParcelableArray("guybrushControInsulti");

        //Ripristino insulti
        gameEngine.getDialogs().loadControInsult(mLevel , true);
        gameEngine.getDialogs().loadInsult(mLevel, true, mInsultiKnow, mControInsultiKnow);
}

Error:

Bound mismatch: The generic method getSparseParcelableArray(String) of type Bundle is not applicable for the arguments (String). The inferred type String is not a valid substitute for the bounded parameter <T extends Parcelable>

What's my problem? :(

Gorgo
  • 456
  • 1
  • 7
  • 19
  • Check this answer: http://stackoverflow.com/questions/14899718/how-to-store-sparsearray-in-bundle – Sagar Waghmare Apr 27 '13 at 13:43
  • So I need to extend SparseArray and implement Parcelable? – Gorgo Apr 27 '13 at 17:36
  • I'vre created a `SparseArrayParcelable extends SparseArray implements Parcelable` but Eclipse says: `The method putSparseParcelableArray(String, SparseArray extends Parcelable>) in the type Bundle is not applicable for the arguments (String, SparseArrayParcelable)` – Gorgo Apr 27 '13 at 17:43

1 Answers1

2

In your case (where the SparseArray is of String), i think you cannot use the method (putSparseParcelableArray).

This method can only be used when the Object type of the Sparse Array implements Parcelable interface, and String class doesn't implement this.

Ref: http://developer.android.com/reference/java/lang/String.html

Till the time we don't get any concrete solution you can do this, While saving the SparseArray, put this data in the list and save that list the outState bundle.

And in onRestoreInstanceState, retrieve the list and convert it to the SparseArray again.

Sagar Waghmare
  • 4,702
  • 1
  • 19
  • 20
  • I've solved implemeting disassemble/builder methods that extract Keys int array from SparseArray and pass it to Bundle. On restore time, builder() builds the SparseArray from keys array ;) – Gorgo Apr 29 '13 at 14:55
  • 1
    Hi @Webserveis You can find the project source here: http://www.int33h.com/test/mi/miapksrc.rar Can't guarantee that things are working as 6y ago though. I stopped working on Android after that project. – Gorgo Mar 15 '19 at 14:10