android.os.Messageใช้Bundleในการส่งด้วยนั้น SendMessage-วิธี ดังนั้นจึงสามารถใส่ a HashMapภายในได้Bundleหรือไม่?
android.os.Messageใช้Bundleในการส่งด้วยนั้น SendMessage-วิธี ดังนั้นจึงสามารถใส่ a HashMapภายในได้Bundleหรือไม่?
คำตอบ:
ลองเป็น:
Bundle extras = new Bundle();
extras.putSerializable("HashMap",hashMap);
intent.putExtras(extras);
และในกิจกรรมที่สอง
Bundle bundle = this.getIntent().getExtras();
if(bundle != null) {
   hashMap = bundle.getSerializable("HashMap");
}
เนื่องจากHashmap   โดยค่าเริ่มต้นใช้งานSerializableเพื่อให้คุณสามารถส่งผ่านโดยใช้putSerializableใน Bundle และเข้าร่วมกิจกรรมอื่น ๆ โดยใช้getSerializable
ตามที่doc , HashmapการดำเนินการSerializableเพื่อให้คุณสามารถputSerializableฉันเดา คุณลองหรือยัง?
โปรดทราบ: หากคุณใช้ AppCompatActivity คุณจะต้องเรียกใช้ 
 เมธอดprotected void onSaveInstanceState(Bundle outState) {}( NOT public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {} )
ตัวอย่างโค้ด ...
จัดเก็บแผนที่:
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable("leftMaxima", leftMaxima);
    outState.putSerializable("rightMaxima", rightMaxima);
}
และรับใน onCreate:
if (savedInstanceState != null) {
    leftMaxima = (HashMap<Long, Float>) savedInstanceState.getSerializable("leftMaxima");
    rightMaxima = (HashMap<Long, Float>) savedInstanceState.getSerializable("rightMaxima");
}
ขออภัยหากเป็นคำตอบที่ซ้ำกัน - อาจมีคนเห็นว่ามีประโยชน์ :)
หากคุณต้องการส่งคีย์ทั้งหมดในกลุ่มคุณสามารถลองได้
for(String key: map.keySet()){
    bundle.putStringExtra(key, map.get(key));
}
      public static Bundle mapToBundle(Map<String, Object> data) throws Exception {
    Bundle bundle = new Bundle();
    for (Map.Entry<String, Object> entry : data.entrySet()) {
        if (entry.getValue() instanceof String)
            bundle.putString(entry.getKey(), (String) entry.getValue());
        else if (entry.getValue() instanceof Double) {
            bundle.putDouble(entry.getKey(), ((Double) entry.getValue()));
        } else if (entry.getValue() instanceof Integer) {
            bundle.putInt(entry.getKey(), (Integer) entry.getValue());
        } else if (entry.getValue() instanceof Float) {
            bundle.putFloat(entry.getKey(), ((Float) entry.getValue()));
        }
    }
    return bundle;
}
    ฉันใช้งาน kotlin ของฉันกับ Parcelable เพื่อให้บรรลุสิ่งนั้นและจนถึงตอนนี้มันก็ใช้ได้ผลสำหรับฉัน จะมีประโยชน์หากคุณต้องการหลีกเลี่ยงการต่ออนุกรมที่หนักหน่วง
นอกจากนี้เพื่อให้ใช้งานได้ฉันขอแนะนำให้ใช้กับสิ่งเหล่านี้
คำประกาศ
class ParcelableMap<K,V>(val map: MutableMap<K,V>) : Parcelable {
    constructor(parcel: Parcel) : this(parcel.readMap(LinkedHashMap<K,V>()))
    override fun writeToParcel(parcel: Parcel, flags: Int) {
        parcel.writeMap(map)
    }
    override fun describeContents(): Int {
        return 0
    }
    companion object CREATOR : Parcelable.Creator<ParcelableMap<Any?,Any?>> {
        @JvmStatic
        override fun createFromParcel(parcel: Parcel): ParcelableMap<Any?,Any?> {
            return ParcelableMap(parcel)
        }
        @JvmStatic 
        override fun newArray(size: Int): Array<ParcelableMap<Any?,Any?>?> {
            return arrayOfNulls(size)
        }
    }
}
ใช้
เขียน
val map = LinkedHashMap<Int, String>()
val wrap = ParcelableMap<Int,String>(map)
Bundle().putParcelable("your_key", wrap)
อ่าน
val bundle = fragment.arguments ?: Bundle()
val wrap = bundle.getParcelable<ParcelableMap<Int,String>>("your_key")
val map = wrap.map
อย่าลืมว่าหากแผนที่ของคุณK,Vไม่ได้รับการแยกส่วนตามค่าเริ่มต้นพวกเขาจะต้องใช้งานParcelable  
ใน Kotlin:
hashMap = savedInstanceState?.getSerializable(ARG_HASH_MAP) as? HashMap<Int, ValueClass>
putSerializable(ARG_HASH_MAP, hashMap)