How to send an object from one Android Activity to

2018-12-31 00:13发布

How can I pass an object of a custom type from one Activity to another using the putExtra() method of the class Intent?

30条回答
十年一品温如言
2楼-- · 2018-12-31 00:48

I know this is late but it is very simple.All you have do is let your class implement Serializable like

public class MyClass implements Serializable{

}

then you can pass to an intent like

Intent intent=......
MyClass obje=new MyClass();
intent.putExtra("someStringHere",obje);

To get it you simpley call

MyClass objec=(MyClass)intent.getExtra("theString");
查看更多
刘海飞了
3楼-- · 2018-12-31 00:49

in your class model (Object) implement Serializable, for Example:

public class MensajesProveedor implements Serializable {

    private int idProveedor;


    public MensajesProveedor() {
    }

    public int getIdProveedor() {
        return idProveedor;
    }

    public void setIdProveedor(int idProveedor) {
        this.idProveedor = idProveedor;
    }


}

and your first Activity

MensajeProveedor mp = new MensajeProveedor();
Intent i = new Intent(getApplicationContext(), NewActivity.class);
                i.putExtra("mensajes",mp);
                startActivity(i);

and your second Activity (NewActivity)

        MensajesProveedor  mensajes = (MensajesProveedor)getIntent().getExtras().getSerializable("mensajes");

good luck!!

查看更多
呛了眼睛熬了心
4楼-- · 2018-12-31 00:50

Another way to do this is to use the Application object (android.app.Application). You define this in you AndroidManifest.xml file as:

<application
    android:name=".MyApplication"
    ...

You can then call this from any activity and save the object to the Application class.

In the FirstActivity:

MyObject myObject = new MyObject();
MyApplication app = (MyApplication) getApplication();
app.setMyObject(myObject);

In the SecondActivity, do :

MyApplication app = (MyApplication) getApplication();
MyObject retrievedObject = app.getMyObject(myObject);

This is handy if you have objects that have application level scope i.e. they have to be used throughout the application. The Parcelable method is still better if you want explicit control over the object scope or if the scope is limited.

This avoid the use of Intents altogether, though. I don't know if they suits you. Another way I used this is to have int identifiers of objects send through intents and retrieve objects that I have in Maps in the Application object.

查看更多
流年柔荑漫光年
5楼-- · 2018-12-31 00:50
Intent i = new Intent();
i.putExtra("name_of_extra", myParcelableObject);
startACtivity(i);
查看更多
萌妹纸的霸气范
6楼-- · 2018-12-31 00:50

Using google's Gson library you can pass object to another activities.Actually we will convert object in the form of json string and after passing to other activity we will again re-convert to object like this

Consider a bean class like this

 public class Example {
    private int id;
    private String name;

    public Example(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

We need to pass object of Example class

Example exampleObject=new Example(1,"hello");
String jsonString = new Gson().toJson(exampleObject);
Intent nextIntent=new Intent(this,NextActivity.class);
nextIntent.putExtra("example",jsonString );
startActivity(nextIntent);

For reading we need to do the reverse operation in NextActivity

 Example defObject=new Example(-1,null);
    //default value to return when example is not available
    String defValue= new Gson().toJson(defObject);
    String jsonString=getIntent().getExtras().getString("example",defValue);
    //passed example object
    Example exampleObject=new Gson().fromJson(jsonString,Example .class);

Add this dependancy in gradle

compile 'com.google.code.gson:gson:2.6.2'
查看更多
冷夜・残月
7楼-- · 2018-12-31 00:51

In your first Activity:

intent.putExtra("myTag", yourObject);

And in your second one:

myCustomObject myObject = (myCustomObject) getIntent().getSerializableExtra("myTag");

Don't forget to make your custom object Serializable:

public class myCustomObject implements Serializable {
...
}
查看更多
登录 后发表回答