Passing object by parameter causes error - Android Studio

0

I'm trying to pass an object by parameter, but an error occurs in the line "it.putExtra (" tag ", obj);":

  Can not resolve method (java.lang.String, my package.minhaClasse)

Follow the code

Obj obj = new Obj();
Intent it;
it = new Intent(this, MinhaClasse.class);
it.putExtra("tag", obj);
startActivity(it);

Another question, how to retrieve this object in the other Activity?

    
asked by anonymous 20.07.2015 / 15:29

1 answer

5

In order for you to pass an object as a parameter it needs to be serialized, that is, implement Serializable . Here is a simple example:
Class:

public class Params implements Serializable{
    public String param1="";
    public String param2="";
    public String param3="";
}

Setting the parameter:

 Params obj = new Params();
        obj.param1 = "Parametro UM ";
        obj.param2 = "Parametro DOIS ";
        obj.param3 = "Parametro TRES ";
        Intent it;
        it = new Intent(context, MainActivity.class);
        it.putExtra("Param", obj);

Greetings

    
20.07.2015 / 15:41