0

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?

user963241
  • 6,758
  • 19
  • 65
  • 93

1 Answers1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • If I use a service to maintain client connection, can it be easily solved? – user963241 Jun 14 '17 at 12:25
  • @user963241: You might need a service, if you want to try to maintain the connection for a while after your UI leaves the foreground. You do not need a service to maintain the connection across a configuration change. – CommonsWare Jun 14 '17 at 12:31
  • Okay, I'll use a service to to maintain the connection for a while after my UI leaves the foreground, but then by using service, would I still need to maintain such connection across a configuration change? In other words, the only solution is by using something like `onRetainNonConfigurationInstance`? – user963241 Jun 14 '17 at 12:50
  • @user963241: "would I still need to maintain such connection across a configuration change?" -- the socket connection would be stable. However, you need to work out how the service and the activity will communicate, and that may require retaining something. – CommonsWare Jun 14 '17 at 13:01
  • Okay. Can you say a bit about why `Socket` and `Thread` cannot be parcelable? – user963241 Jun 14 '17 at 13:12
  • @user963241: They cannot be passed across process boundaries, which is the complete and entire point behind implementing `Parcelable`. Neither can an `Activity` or a `TextView`, for that matter. – CommonsWare Jun 14 '17 at 14:00
  • So, I call `setRetainInstance(true)` in OnCreate method for my class that extends from `Fragment` but it doesn't retain anything on rotation. E.g. text is TextView is not retained or sockets. I saw the example [here](http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html) can you help? – user963241 Jun 14 '17 at 20:03
  • @user963241: Your retained fragment needs to be set up via a `FragmentTransaction`, not a `` tag. See [this sample app](https://github.com/commonsguy/cw-omnibus/tree/master/ConfigChange/Fragments). If you have further problems with retained fragments, ask a separate Stack Overflow question with a [mcve]. – CommonsWare Jun 14 '17 at 20:06
  • help me https://stackoverflow.com/questions/44550203/how-to-get-different-data-everytime-when-using-parceable-by-tranfering-the-objec –  Jun 15 '17 at 00:20