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

后端 未结 30 4526
-上瘾入骨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: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");
    

提交回复
热议问题