Java – whether custom objects are passed by value or by reference in fragments and activities

Hello, Java and Android experts I have a problem.

I have a room_ Class of structure, which implements serializable This class has an object called currentroom

If I put the object between two fragments between two fragments and put it in a bundle, it works normally. Surprisingly, it is passed by reference I don't know why It shouldn't do that BTW am I using the Android support library?

However, if I am using a bundle to pass the currentroom object between activities to an intention, I will encounter a crash when I try to start a new activity with that intention

More description this is the 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 classes have constructors, getters and setters

Now this is what I'm doing:

Consider that the currentroom object has been populated. Here is how I pass it to the 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();

From fragment_ Retrieve it from the chat onactivitycreated() method

Bundle extras = getArguments();
Room_Structure recievedRoom = (Room_Structure) extras.getSerializable("RoomObject");

Now the above code is working for the fragment The only problem is passing a reference to the object to the new fragment Which should not be real behavior It should just send values without reference

This is how I know that objects are passed by reference

Sending Object : com.software.chat.Classes.Room_Structure@425585e8
Recieved Object: com.software.chat.Classes.Room_Structure@425585e8

All have the same reference or address Check it out during commissioning

Now I can't copy this behavior in the activity

I have an activity that contains an expandablelistview I have implemented the adapter by extending the list of extensions from the baseextendable listadapter The adapter class name is websites_ listadapter. java. It also passes the context of the activity in its constructor I have clicked on the layout in the list And when I click, I want to start an activity Please don't ask why I do this is a long story I'm from this website_ Listadapter sends this object

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 I received this error in logcat. This time when I called 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):    at android.os.Parcel.writeSerializable(Parcel.java:1279)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.os.Parcel.writeValue(Parcel.java:1233)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.os.Parcel.writeMapInternal(Parcel.java:591)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.os.Bundle.writeToParcel(Bundle.java:1619)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.os.Parcel.writeBundle(Parcel.java:605)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.content.Intent.writeToParcel(Intent.java:6814)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.app.ActivityManagerProxy.startActivity

(ActivityManagerNative.java:1910)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1415)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.app.Activity.startActivityForResult(Activity.java:3446)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.app.Activity.startActivityForResult(Activity.java:3407)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.support.v4.app.FragmentActivity.startActivityForResult

(FragmentActivity.java:817)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.app.Activity.startActivity(Activity.java:3617)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.app.Activity.startActivity(Activity.java:3585)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at com.software.chat.Adapters.Websites_listadapter$1.onClick

(Websites_listadapter.java:211)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.view.View.performClick(View.java:4211)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.view.View$PerformClick.run(View.java:17267)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.os.Handler.handleCallback(Handler.java:615)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.os.Looper.loop(Looper.java:137)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.app.ActivityThread.main(ActivityThread.java:4898)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at java.lang.reflect.Method.invokeNative(Native Method)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at java.lang.reflect.Method.invoke(Method.java:511)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run

(ZygoteInit.java:1006)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at 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):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at java.io.ObjectOutputStream.writeObjectInternal

(ObjectOutputStream.java:1671)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at java.io.ObjectOutputStream.defaultWriteObject

(ObjectOutputStream.java:368)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at java.io.ObjectOutputStream.writeObjectInternal

(ObjectOutputStream.java:1671)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481)
04-25 15:38:07.474: E/AndroidRuntime(10250):    at android.os.Parcel.writeSerializable(Parcel.java:1274)
04-25 15:38:07.474: E/AndroidRuntime(10250):    ... 24 more

I know there are many ways to pass objects between activities, but I want to know why this happens and what is serializable in the background?

Any help in this matter will be greatly appreciated

Solution

The difference is that instead of creating an active object like a fragment, the system is required to do so You create an intention to require the system to create an activity. Everything in the intention is serialized and passed to the new activity through the system It is impossible to create two activities by sharing the same object at the same time If you really need the same object reference, you can create a singleton at any time

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>