How to create Serializable object and pass to bundle.putSerializable(key, value)

柔情痞子 提交于 2019-12-24 13:38:30

问题


I have an activity which has an object of type mqttQndroidClient. this object i would like to pass it to another activity. To achieve this i read about how to solve such issue and the soltion i found was to create a class that implements serializable as shown below, and i did the following:

client = new mqttAndroidClient(..,...,..,..,);
Intent i = new Intent(act_1.this, act_2.class)
clientObject = clientObj = new (CLIENT_OBJ_KEY, client);
Bundle b = new Bundle();
b.putSerializable(CLIENT_OBJ_KEY, clientOb.getObjValue());

But eclipse underscore the b.putSerializable(CLIENT_OBJ_KEY, clientObjValue()); with red, it seems i did not cretae a serializable object correctly.

and the clientObject class that implements serializable, looks as below:

public class ClientObject implements Serializable {

    private String objectKey;
    private MqttAndroidClient objectValue;

    public ClientObject(String objectKey, MqttAndroidClient objectValue) {

        this.objectKey = objectKey;
        this.objectValue = objectValue;
    }

    public void setObjKey(String objectKey) {

        this.objectKey = objectKey;
    }

    public String getObjKey() {

        return this.objectKey;
    }

    public void setObjValue(MqttAndroidClient objValue) {

        this.objectValue = objValue;
    }

    public MqttAndroidClient getObjValue() {

        return this.objectValue;
    }
}

回答1:


I see many problems in your solution. The biggest one is: you can not encapsulate a not serializable object inside a serializable one and hope that thing will work. To be serializable every single field must be serializable, please read the manual.

What problem are you addressing here?

  1. Is it expensive to create a client for every activity (in terms of time/memory)?
  2. Do you not want to reconfigure the client every time?
  3. Do you want a single instance around your application?

My opinion about the first case is that is not to expensive to create a client when you need one (one per activity I suppose), by reading the documentation about MqttAndroidClient I understand that the client is using a long running service. This service is responsible for the client/server communications, so, I assume, the client is a very tiny wrapper to simplify service/activity communications. This imply you can create as many client as you want.

This leads us to the second point: you don't want to reconfigure the client every time. The solution is to create a factory. Basically something like this:

public final class MqttClientFactory {
  public static MqttAndroidClient createClientInstance(Context context, ...) {
    MqttAndroidClient client = new MqttAndroidClient(context, ....);
    // Configure your client here
    return client;
  }
}

Now you can create a configured instance your Activity.onCreate(...)

@Override
protected void onCreate (Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ...
  mMqttClient = MqttClientFactory.createClientInstance(this, ...);
  ...
}

If you want a single instance of you client for the entire app, there are many solutions, that generally involve the use of a singleton. I personally prefer to avoid singletons, so I suggest to override the Application class with your own, create an instance of your client there and get the reference to it from your activity.

public class MyApplication extends Application {
  private MqttAndroidClient mClient;

  public void onCreate () {
    super.onCreate();

    mClient = new MqttAndroidClient(this, ...);

    ...
  }

  public final MqttAndroidClient getClient() {
    return mClient;
  }
}

You can now use Context.getApplicationContext() or Acticity.getAppliaction() to get a reference to your custom Application. I.E.:

MqttAndroidClient sharedClient = ((MyApplication) getApplication()).getClient();

You need to add your custom application to the manifest, please see the docs here




回答2:


Does the method clientObjValue() return an object ClientObject? Is the class MqttAndroidClient also serializable?



来源:https://stackoverflow.com/questions/27122149/how-to-create-serializable-object-and-pass-to-bundle-putserializablekey-value

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