How to implement Parcelable object for my class?

徘徊边缘 提交于 2019-12-11 15:26:37

问题


I have class which is used to create Client that connects to a Server. It has the following members:

public class MyClientThread implements Runnable, Parcelable {

    private TextView ClientServerMsg;
    private Integer PortNumber = 4545;
    private String IPAddress = "127.0.0.1";
    private Activity activity;
    private Socket clientSocket;
}

When the user rotates the screen, all the client object data is lost/reset and the client has to be reconnected with server.

While implementing writeToParcel method, I ran into problems i.e. How to parcel Socket and Thread class object and such?


回答1:


How to parcel Socket and Thread class object and such?

You can't.

When the user rotates the screen, all the client object data is lost/reset and the client has to be reconnected with server.

Use a retained fragment, or onRetainNonConfigurationInstance(), or something based off of the lifecycle architecture components (e.g., possibly LiveData), to retain this across configuration changes.



来源:https://stackoverflow.com/questions/44544417/how-to-implement-parcelable-object-for-my-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!