Android:如何传递 class 对意图的引用
Android: how to pass class reference to intent
我想传递对意图的 class 引用,以便加载 Activity 中我的 class 的实例。
请考虑我将 "reference" 写到 class,所以它不应该是另一个实例。按照所有说明,我发现我能够通过 class 但它最终是另一个实例。
intent.putExtra("idletime", idle);
空闲是我的 class 我想作为参考。
我从哪里得到它:
idle_ = getIntent().getParcelableExtra("idletime");
但似乎引用是针对我的 class 的新实例,而不是我试图传递的 class 实例。
这是 class 的定义,但我认为不是那么重要:
public class idletime extends AsyncTask<Object, Integer, Boolean> implements Parcelable {}
有什么问题吗?
but it seems that the reference is for a new instance of my class and not the same class instance I tried to pass.
这是正确的行为,putExtra
会将您的 class 转换为二进制形式(序列化),然后 getParcelableExtra
会将您的 class 从二进制形式反序列化,这方法创建一个新实例。
我想传递对意图的 class 引用,以便加载 Activity 中我的 class 的实例。 请考虑我将 "reference" 写到 class,所以它不应该是另一个实例。按照所有说明,我发现我能够通过 class 但它最终是另一个实例。
intent.putExtra("idletime", idle);
空闲是我的 class 我想作为参考。 我从哪里得到它:
idle_ = getIntent().getParcelableExtra("idletime");
但似乎引用是针对我的 class 的新实例,而不是我试图传递的 class 实例。
这是 class 的定义,但我认为不是那么重要:
public class idletime extends AsyncTask<Object, Integer, Boolean> implements Parcelable {}
有什么问题吗?
but it seems that the reference is for a new instance of my class and not the same class instance I tried to pass.
这是正确的行为,putExtra
会将您的 class 转换为二进制形式(序列化),然后 getParcelableExtra
会将您的 class 从二进制形式反序列化,这方法创建一个新实例。