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

后端 未结 30 3579
-上瘾入骨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:31

    the most easiest solution i found is.. to create a class with static data members with getters setters.

    set from one activity and get from another activity that object.

    activity A

    mytestclass.staticfunctionSet("","",""..etc.);
    

    activity b

    mytestclass obj= mytestclass.staticfunctionGet();
    
    0 讨论(0)
  • 2020-11-21 05:31

    First implement Parcelable in your class. Then pass object like this.

    SendActivity.java

    ObjectA obj = new ObjectA();
    
    // Set values etc.
    
    Intent i = new Intent(this, MyActivity.class);
    i.putExtra("com.package.ObjectA", obj);
    
    startActivity(i);
    

    ReceiveActivity.java

    Bundle b = getIntent().getExtras();
    ObjectA obj = b.getParcelable("com.package.ObjectA");
    

    The package string isn't necessary, just the string needs to be the same in both Activities

    REFERENCE

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

    if your object class implements Serializable, you don't need to do anything else, you can pass a serializable object.
    that's what i use.

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

    implement serializable in your class

    public class Place implements Serializable{
            private int id;
            private String name;
    
            public void setId(int id) {
               this.id = id;
            }
            public int getId() {
               return id;
            }
            public String getName() {
               return name;
            }
    
            public void setName(String name) {
               this.name = name;
            }
    }
    

    Then you can pass this object in intent

         Intent intent = new Intent(this, SecondAct.class);
         intent.putExtra("PLACE", Place);
         startActivity(intent);
    

    int the second activity you can get data like this

         Place place= (Place) getIntent().getSerializableExtra("PLACE");
    

    But when the data become large,this method will be slow.

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

    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");
    
    0 讨论(0)
  • 2020-11-21 05:34

    If you are not very particular about using the putExtra feature and just want to launch another activity with objects, you can check out the GNLauncher (https://github.com/noxiouswinter/gnlib_android/wiki#gnlauncher) library I wrote in an attempt to make this process more straight forward.

    GNLauncher makes sending objects/data to an Activity from another Activity etc as easy as calling a function in the Activity with the required data as parameters. It introduces type safety and removes all the hassles of having to serialize, attaching to the intent using string keys and undoing the same at the other end.

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