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

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

    Thanks for parcelable help but i found one more optional solution

     public class getsetclass implements Serializable {
            private int dt = 10;
        //pass any object, drwabale 
            public int getDt() {
                return dt;
            }
    
            public void setDt(int dt) {
                this.dt = dt;
            }
        }
    

    In Activity One

    getsetclass d = new getsetclass ();
                    d.setDt(50);
                    LinkedHashMap obj = new LinkedHashMap();
                    obj.put("hashmapkey", d);
                Intent inew = new Intent(SgParceLableSampelActivity.this,
                        ActivityNext.class);
                Bundle b = new Bundle();
                b.putSerializable("bundleobj", obj);
                inew.putExtras(b);
                startActivity(inew);
    

    Get Data In Activity 2

     try {  setContentView(R.layout.main);
                Bundle bn = new Bundle();
                bn = getIntent().getExtras();
                HashMap getobj = new HashMap();
                getobj = (HashMap) bn.getSerializable("bundleobj");
                getsetclass  d = (getsetclass) getobj.get("hashmapkey");
            } catch (Exception e) {
                Log.e("Err", e.getMessage());
            }
    

提交回复
热议问题