How to pass gson serialised object to Intent in android?

前端 未结 3 685
南笙
南笙 2020-12-16 01:55

i am trying to pass the gson serialised object to intent by using the below code

intent.putExtra(\"com.example\", vo); // vo is the gson

3条回答
  •  独厮守ぢ
    2020-12-16 02:19

    I added implements Serializable to my model AND other models (sub-models) used by my model. Then I can pass the GSON object via Intent:

    public class MyModel implements Serializable {
        SubModel subModel;
    }
    
    public class SubModel implements Serializable {
        ...
    }
    

    In fragment:

    Intent startIntent = new Intent(getContext(), NextActivity.class);
    startIntent.putExtra("mymodel", myModelObject);
    startActivity(startIntent);
    

    In next activity:

    Intent intent = getIntent();
    MyModel mymodel = (MyModel) intent.getSerializableExtra("mymodel");
    // test if we get the model correctly:
    setTitle(mymodel.getName());
    

提交回复
热议问题