How to send an object from one Android Activity to another using Intents?

后端 未结 30 3686
-上瘾入骨i
-上瘾入骨i 2020-11-21 04:47

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

相关标签:
30条回答
  • 2020-11-21 05:24

    I use Gson with its so powerful and simple api to send objects between activities,

    Example

    // This is the object to be sent, can be any object
    public class AndroidPacket {
    
        public String CustomerName;
    
       //constructor
       public AndroidPacket(String cName){
           CustomerName = cName;
       }   
       // other fields ....
    
    
        // You can add those functions as LiveTemplate !
        public String toJson() {
            Gson gson = new Gson();
            return gson.toJson(this);
        }
    
        public static AndroidPacket fromJson(String json) {
            Gson gson = new Gson();
            return gson.fromJson(json, AndroidPacket.class);
        }
    }
    

    2 functions you add them to the objects that you want to send

    Usage

    Send Object From A to B

        // Convert the object to string using Gson
        AndroidPacket androidPacket = new AndroidPacket("Ahmad");
        String objAsJson = androidPacket.toJson();
    
        Intent intent = new Intent(A.this, B.class);
        intent.putExtra("my_obj", objAsJson);
        startActivity(intent);
    

    Receive In B

    @Override
    protected void onCreate(Bundle savedInstanceState) {        
        Bundle bundle = getIntent().getExtras();
        String objAsJson = bundle.getString("my_obj");
        AndroidPacket androidPacket = AndroidPacket.fromJson(objAsJson);
    
        // Here you can use your Object
        Log.d("Gson", androidPacket.CustomerName);
    }
    

    I use it almost in every project i do and I have no performance issues.

    0 讨论(0)
  • 2020-11-21 05:25

    I struggled with the same problem. I solved it by using a static class, storing any data I want in a HashMap. On top I use an extension of the standard Activity class where I have overriden the methods onCreate an onDestroy to do the data transport and data clearing hidden. Some ridiculous settings have to be changed e.g. orientation-handling.

    Annotation: Not providing general objects to be passed to another Activity is pain in the ass. It's like shooting oneself in the knee and hoping to win a 100 metres. "Parcable" is not a sufficient substitute. It makes me laugh... I don't want to implement this interface to my technology-free API, as less I want to introduce a new Layer... How could it be, that we are in mobile programming so far away from modern paradigm...

    0 讨论(0)
  • 2020-11-21 05:26

    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!!

    0 讨论(0)
  • 2020-11-21 05:27

    By far the easiest way IMHO to parcel objects. You just add an annotation tag above the object you wish to make parcelable.

    An example from the library is below https://github.com/johncarl81/parceler

    @Parcel
    public class Example {
        String name;
        int age;
    
        public Example(){ /*Required empty bean constructor*/ }
    
        public Example(int age, String name) {
            this.age = age;
            this.name = name;
        }
    
        public String getName() { return name; }
    
        public int getAge() { return age; }
    }
    
    0 讨论(0)
  • 2020-11-21 05:28

    you can use putExtra(Serializable..) and getSerializableExtra() methods to pass and retrieve objects of your class type; you will have to mark your class Serializable and make sure that all your member variables are serializable too...

    0 讨论(0)
  • 2020-11-21 05:29

    You can use android BUNDLE to do this.

    Create a Bundle from your class like:

    public Bundle toBundle() {
        Bundle b = new Bundle();
        b.putString("SomeKey", "SomeValue");
    
        return b;
    }
    

    Then pass this bundle with INTENT. Now you can recreate your class object by passing bundle like

    public CustomClass(Context _context, Bundle b) {
        context = _context;
        classMember = b.getString("SomeKey");
    }
    

    Declare this in your Custom class and use.

    0 讨论(0)
提交回复
热议问题