java - Are Custom Objects Passed by value or passed by Reference in Fragments and Activities.? -
hello java , android experts. having problem. making xmpp based chat application. there few things confusing me.
i have class named room_structure implements serializable. class has object named currentroom.
if passing currentroom object between 2 fragments putting in bundle working fine , surprisingly passed reference. dont know why that. shouldnt behave this. btw using android support library?
but if passing currentroom object between activities using bundle , putting bundle in intent getting crash whenever try start new activity using intent.
for more description here code
public class room_structure implements serializable { private static final long serialversionuid = 1l; private string rname; private arraylist<message_pattern> msg_list; private multiuserchat xmppsession; private boolean background; private boolean modified; private boolean destroyed; }
the above class has constructors getter , setters.
now here doing:
consider currentroom object has been populated , here how passing fragment
bundle b = new bundle(); b.putserializable("roomobject", currentroom); fragment_chat newchat = new fragment_chat(); newchat.setarguments(b); fragmentmanager fm = getchildfragmentmanager(); fragmenttransaction ft = fm.begintransaction(); ft.replace(r.id.fl_chatfragment, newchat); ft.addtobackstack(null); ft.commit();
retrieving fragment_chat onactivitycreated() method this
bundle extras = getarguments(); room_structure recievedroom = (room_structure) extras.getserializable("roomobject");
now above code working fine fragments. problem object's reference passed new fragment. not how should behave. should send value not reference.
here how know objects passed reference
sending object : com.software.chat.classes.room_structure@425585e8 recieved object: com.software.chat.classes.room_structure@425585e8
both have same reference or address. checked out during debugging
now cant replicate behavior in activities
i have activity contains expandiblelistview. have implemented adapter expandiblelistview extending baseexpandablelistadapter. adapter class name websites_listadapter.java. passed activity's context in constructor. , have made layout clickable in list. , want start activity when clicked. please don't ask why did long story. sending object websites_listadapter this
intent i=new intent(activitycontext, chatscreen.class); bundle b = new bundle(); b.putserializable("roomobject", currentroom); i.putextras(b); i.setflags(intent.flag_activity_clear_top); activitycontext.startactivity(i);
but error in logcat time when call activitycontext.startactivity(i)
04-25 15:38:07.474: e/androidruntime(10250): fatal exception: main 04-25 15:38:07.474: e/androidruntime(10250): java.lang.runtimeexception: parcelable encountered ioexception writing serializable object (name = com.software.chat.classes.room_structure) 04-25 15:38:07.474: e/androidruntime(10250): @ android.os.parcel.writeserializable(parcel.java:1279) 04-25 15:38:07.474: e/androidruntime(10250): @ android.os.parcel.writevalue(parcel.java:1233) 04-25 15:38:07.474: e/androidruntime(10250): @ android.os.parcel.writemapinternal(parcel.java:591) 04-25 15:38:07.474: e/androidruntime(10250): @ android.os.bundle.writetoparcel(bundle.java:1619) 04-25 15:38:07.474: e/androidruntime(10250): @ android.os.parcel.writebundle(parcel.java:605) 04-25 15:38:07.474: e/androidruntime(10250): @ android.content.intent.writetoparcel(intent.java:6814) 04-25 15:38:07.474: e/androidruntime(10250): @ android.app.activitymanagerproxy.startactivity (activitymanagernative.java:1910) 04-25 15:38:07.474: e/androidruntime(10250): @ android.app.instrumentation.execstartactivity(instrumentation.java:1415) 04-25 15:38:07.474: e/androidruntime(10250): @ android.app.activity.startactivityforresult(activity.java:3446) 04-25 15:38:07.474: e/androidruntime(10250): @ android.app.activity.startactivityforresult(activity.java:3407) 04-25 15:38:07.474: e/androidruntime(10250): @ android.support.v4.app.fragmentactivity.startactivityforresult (fragmentactivity.java:817) 04-25 15:38:07.474: e/androidruntime(10250): @ android.app.activity.startactivity(activity.java:3617) 04-25 15:38:07.474: e/androidruntime(10250): @ android.app.activity.startactivity(activity.java:3585) 04-25 15:38:07.474: e/androidruntime(10250): @ com.software.chat.adapters.websites_listadapter$1.onclick (websites_listadapter.java:211) 04-25 15:38:07.474: e/androidruntime(10250): @ android.view.view.performclick(view.java:4211) 04-25 15:38:07.474: e/androidruntime(10250): @ android.view.view$performclick.run(view.java:17267) 04-25 15:38:07.474: e/androidruntime(10250): @ android.os.handler.handlecallback(handler.java:615) 04-25 15:38:07.474: e/androidruntime(10250): @ android.os.handler.dispatchmessage(handler.java:92) 04-25 15:38:07.474: e/androidruntime(10250): @ android.os.looper.loop(looper.java:137) 04-25 15:38:07.474: e/androidruntime(10250): @ android.app.activitythread.main(activitythread.java:4898) 04-25 15:38:07.474: e/androidruntime(10250): @ java.lang.reflect.method.invokenative(native method) 04-25 15:38:07.474: e/androidruntime(10250): @ java.lang.reflect.method.invoke(method.java:511) 04-25 15:38:07.474: e/androidruntime(10250): @ com.android.internal.os.zygoteinit$methodandargscaller.run (zygoteinit.java:1006) 04-25 15:38:07.474: e/androidruntime(10250): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:773) 04-25 15:38:07.474: e/androidruntime(10250): @ dalvik.system.nativestart.main(native method) 04-25 15:38:07.474: e/androidruntime(10250): caused by: java.io.notserializableexception: org.jivesoftware.smackx.muc.multiuserchat 04-25 15:38:07.474: e/androidruntime(10250): @ java.io.objectoutputstream.writenewobject(objectoutputstream.java:1364) 04-25 15:38:07.474: e/androidruntime(10250): @ java.io.objectoutputstream.writeobjectinternal (objectoutputstream.java:1671) 04-25 15:38:07.474: e/androidruntime(10250): @ java.io.objectoutputstream.writeobject(objectoutputstream.java:1517) 04-25 15:38:07.474: e/androidruntime(10250): @ java.io.objectoutputstream.writeobject(objectoutputstream.java:1481) 04-25 15:38:07.474: e/androidruntime(10250): @ java.io.objectoutputstream.writefieldvalues(objectoutputstream.java:979) 04-25 15:38:07.474: e/androidruntime(10250): @ java.io.objectoutputstream.defaultwriteobject (objectoutputstream.java:368) 04-25 15:38:07.474: e/androidruntime(10250): @ java.io.objectoutputstream.writehierarchy(objectoutputstream.java:1074) 04-25 15:38:07.474: e/androidruntime(10250): @ java.io.objectoutputstream.writenewobject(objectoutputstream.java:1404) 04-25 15:38:07.474: e/androidruntime(10250): @ java.io.objectoutputstream.writeobjectinternal (objectoutputstream.java:1671) 04-25 15:38:07.474: e/androidruntime(10250): @ java.io.objectoutputstream.writeobject(objectoutputstream.java:1517) 04-25 15:38:07.474: e/androidruntime(10250): @ java.io.objectoutputstream.writeobject(objectoutputstream.java:1481) 04-25 15:38:07.474: e/androidruntime(10250): @ android.os.parcel.writeserializable(parcel.java:1274) 04-25 15:38:07.474: e/androidruntime(10250): ... 24 more
i know there many ways pass objects between activities want know why happening way , serializable in background ?
any regarding matter appreciated.
the difference don't create activity object fragments, ask system it. create intent in order ask system create activity, every thing in intent serialized , passed through system new activity. not possible create 2 activities @ same time sharing same object. if need same object reference can create singleton.