如何创建序列化对象,并传递给bundle.putSerializable(键,值) [英] How to create Serializable object and pass to bundle.putSerializable(key, value)
问题描述
我也有类型的对象的活动 mqttQndroidClient
。这个对象我想将它传递给另一个活动。为了实现这一点,我阅读有关如何解决这样的问题,我发现soltion是创建一个类实现序列化
如下图所示,和我做了以下内容:
客户端=新mqttAndroidClient(..,...,...,..,);
意图I =新意图(act_1.this,act_2.class)
clientObject = clientObj =新(CLIENT_OBJ_KEY,客户端);
束B =新包();
b.putSerializable(CLIENT_OBJ_KEY,clientOb.getObjValue());
但是Eclipse强调 b.putSerializable(CLIENT_OBJ_KEY,clientObjValue());
有红色,看来我没cretae一个序列化
对象正确。
和 clientObject
类,它实现序列化
,看上去如下:
公共类ClientObject实现Serializable { 私人字符串objectKey;
私人MqttAndroidClient objectValue; 公共ClientObject(字符串objectKey,MqttAndroidClient objectValue){ this.objectKey = objectKey;
this.objectValue = objectValue;
} 公共无效setObjKey(字符串objectKey){ this.objectKey = objectKey;
} 公共字符串getObjKey(){ 返回this.objectKey;
} 公共无效setObjValue(MqttAndroidClient objValue){ this.objectValue = objValue;
} 公共MqttAndroidClient getObjValue(){ 返回this.objectValue;
}
}
我看到你解决很多问题。最大的一个是:你不能封装一个序列化的1个内没有一个序列化对象,并希望事情会工作。可序列化,每一个字段必须是可序列化,请阅读使用说明书。
问题是什么,你在这里解决?
- 是它昂贵创建客户端的每个活动(时间/内存方面)?
- 请你不想每次都重新配置客户端?
- 你想在你的应用程序中的单个实例?
我对第一种情况的观点是,不昂贵创建一个客户端在您需要时(每个活动我猜想之一),通过阅读<一href=\"http://www.eclipse.org/paho/files/android-javadoc/org/eclipse/paho/android/service/MqttAndroidClient.html\">documentation有关MqttAndroidClient据我所知,客户端使用长时间运行的服务。此服务负责客户端/服务器通信,所以,我认为,客户端是一个非常小的包装,以简化服务/活动的通信。这意味着,只要你想,你可以创建任意多个客户端。
这使我们的第二点:你不希望每次都重新配置客户端。
该解决方案是创建一个工厂。基本上是这样的:
公共final类MqttClientFactory {
公共静态MqttAndroidClient createClientInstance(上下文的背景下,...){
MqttAndroidClient客户端=新MqttAndroidClient(背景下,....);
//这里配置客户端
返回客户端;
}
}
现在您可以创建一个配置实例的<一个href=\"http://developer.android.com/reference/android/app/Activity.html#onCreate%28android.os.Bundle%29\">Activity.onCreate(...)
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
...
mMqttClient = MqttClientFactory.createClientInstance(此,...);
...
}
如果你想你的整个应用程序客户端的一个实例,有很多的解决方案,这通常涉及使用的单。我个人preFER避免单身,所以我建议重写应用程序类用自己,创造你的客户的实例有,并从活动得到引用。
公共类MyApplication的扩展应用{
私人MqttAndroidClient mClient; 公共无效的onCreate(){
super.onCreate(); mClient =新MqttAndroidClient(此,...); ...
} 公众最终MqttAndroidClient getClient(){
返回mClient;
}
}
您现在可以使用<一个href=\"http://developer.android.com/reference/android/content/Context.html#getApplicationContext()\">Context.getApplicationContext()或<一个href=\"http://developer.android.com/reference/android/app/Activity.html#getApplication%28%29\">Acticity.getAppliaction()去你的自定义应用程序的参考。即:
MqttAndroidClient sharedClient =((所有MyApplication)getApplication())getClient()。
您需要将您的自定义应用程序添加到清单,请参阅文档这里
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;
}
}
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?
- Is it expensive to create a client for every activity (in terms of time/memory)?
- Do you not want to reconfigure the client every time?
- 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
这篇关于如何创建序列化对象,并传递给bundle.putSerializable(键,值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!