Analysis of deep and shallow copy in the process of communication serialization between activities under Android

preface

The background of the problem is that the video interactive service needs to add the bullet screen function, but the view of the player is pseudo horizontal screen, that is, it rotates the horizontal screen in a way similar to using rotate (90.0), and it is still a vertical screen on the activity level. Then the keyboard when the bullet screen is input is also vertical. This will bring serious user experience problems.

Because the screen rotation status is an activity level thing in Android, and it is quite low-level, there is no hook. After many investigations, we decided to pick up a horizontal screen activity as a special activity for keyboard input.

The code here can be written quickly as follows:

The code definition of dto is as follows:

So now the question is, how to bring back the string obtained by this activity?

The most natural idea is onactivityresult. However, the player is an SDK and cannot write the code in the activity, nor can it notify many business parties to make changes one by one.

Then we can only put aside the communication mechanism between Android native activities and think about other methods that can communicate. Naturally, we thought of callback. The structure is shown in the figure below. But how can an object of non basic data type such as callback be passed between activities?

Try to store the extras of intent. However, the putextra method cannot put an object, but only a serializable. Let the dto (data transfer object) implements serializable interface. no problem.

However, if the activity cannot be started, a crash will be thrown:

The error stack is as follows:

If you change the member variable of this dto to static type, you can start the activity.

The reason behind this is that in the normal serialization process, shallow copies are meaningless. Shallow copy means copying a referenced address, which is a memory address. However, conventional serialization, either cross process or network transmission, is serialized into JSON. In these conventional scenarios, memory addresses are meaningless. Therefore, Java serialization does not have the option of shallow copy, and it is often serialized for a POJO or bean instead of a general class with many references.

However, the transfer objects between activities and activities in Android are different. Theoretically, they all run in the same Dalvik VM, and the mutual class references can be accessed. However, because Android intent is designed for serialization transmission, there is no shallow copy mechanism in the serialization process, so it is impossible to pass references to the past in shallow copy.

So why can it be passed after it is set to static without causing a crash? Because static members belong to the class level, although they cannot be serialized, because I am on the same machine (and the same process), my JVM has loaded this class together with its static variables, so what I get is the static variable address at the class level. Therefore, the function is normal.

Then decide to use public static WeakReference < dwdanmakuwritecontroller. Danmakuwritecallback > callback; Yes. But in fact, there is another problem:

During the first startactivity, it was observed that Android performed a GC, and then the WeakReference was released, so the business function of callback could not be executed normally. WeakReference was originally introduced to avoid possible memory leakage caused by static caklback. However, in this case of active GC, WeakReference fails. If you use softreference instead, it is no different from strong reference, and memory leakage cannot be avoided.

Finally, use atomreference to hold the static callback, and set atomicreference to null when the activity exits. The reason for using atomicreference is that considering the possibility of concurrent scenes in the video SDK, the possibility of setting null while preparing for use is avoided.

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

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
分享
二维码
< <上一篇
下一篇>>