如何为我的班级实现 Parcelable 对象? [英] How to implement Parcelable object for my class?

查看:55
本文介绍了如何为我的班级实现 Parcelable 对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,用于创建连接到服务器的客户端.它有以下成员:

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.

在实现 writeToParcel 方法时,我遇到了一些问题,即如何打包 SocketThread 类对象等等?

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

推荐答案

如何打包Socket和Thread类对象之类的?

How to parcel Socket and Thread class object and such?

你不能.

当用户旋转屏幕时,所有客户端对象数据丢失/重置,客户端必须重新连接服务器.

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

使用保留片段或 onRetainNonConfigurationInstance(),或基于 生命周期架构组件(例如,可能是LiveData),以在配置更改时保留这一点.

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.

这篇关于如何为我的班级实现 Parcelable 对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆