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

后端 未结 30 3596
-上瘾入骨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: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; }
    }
    

提交回复
热议问题