Passing data through intent using Serializable

前端 未结 11 1637
借酒劲吻你
借酒劲吻你 2020-11-22 13:35

I\'ve implemented my class with serializable, but it still didn\'t work.

This is my class:

package com.ursabyte.thumbnail;

import java.io.Serializab         


        
11条回答
  •  不知归路
    2020-11-22 13:45

    Don't forget to implement Serializable in every class your object will use like a list of objects. Else your app will crash.

    Example:

    public class City implements Serializable {
    
    private List house;
    
    public List getHouse() {
        return house;
    }
    
    public void setHouse(List house) {
        this.house = house;
    }}
    

    Then House needs to implements Serializable as so :

    public class House implements Serializable {
    
    private String name;
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }}
    

    Then you can use:

    Bundle bundle = new Bundle();
    bundle.putSerializable("city", city);
    intent.putExtras(bundle);
    

    And retreive it with:

    Intent intent = this.getIntent();
    Bundle bundle = intent.getExtras();
    City city =  (City)bundle.getSerializable("city");
    

提交回复
热议问题