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

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

提交回复
热议问题