Search code examples
androidparcelable

How to implement Parcelable object for my class?


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?


Solution

  • 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.