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

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

    Short answer for fast need

    1. Implement your Class to Serializable.

    If you have any inner Classes don't forget to implement them to Serializable too!!

    public class SportsData implements  Serializable
    public class Sport implements  Serializable
    
    List clickedObj;
    

    2. Put your object into Intent

     Intent intent = new Intent(SportsAct.this, SportSubAct.class);
                intent.putExtra("sport", clickedObj);
                startActivity(intent);
    

    3. And receive your object in the other Activity Class

    Intent intent = getIntent();
        Sport cust = (Sport) intent.getSerializableExtra("sport");
    

提交回复
热议问题