How to pass gson serialised object to Intent in android?

前端 未结 3 686
南笙
南笙 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:32

    No you are using it in the wrong way.

    Put the object in the intent as:

    Gson gson = new Gson();
    Intent intent = new Intent(Source.this, Target.class);
    intent.putExtra("obj", gson.toJson(yourObject));
    

    and get the object in another activity as:

    Gson gson = new Gson();
    String strObj = getIntent().getStringExtra("obj");
    SourceObject obj = gson.fromJson(strObj, SourceObject.class);
    

提交回复
热议问题