我正在尝试从一个“活动”发送客户类的对象,并在另一个“”中显示它。
客户类别的代码:
public class Customer {
private String firstName, lastName, address;
int age;
public Customer(String fname, String lname, int age, String address) {
firstName = fname;
lastName = lname;
age = age;
address = address;
}
public String printValues() {
String data = null;
data = "First Name :" + firstName + " Last Name :" + lastName
+ " Age : " + age + " Address : " + address;
return data;
}
}
我想将其对象从一个“活动”发送到另一个“,然后在另一个活动”上显示数据。
我怎样才能做到这一点?
Android活动对象可以被销毁和重建。因此,您需要使用另一种方法来查看它们或它们创建的任何对象!!!-向上的也就是说,您可以作为静态类引用传递,但对象句柄(Java调用这些“引用”,SmallTalk也是如此;但它们不是C或汇编意义上的引用)稍后可能会无效,因为Android OE的一个“特性”是任何活动都可以在稍后被销毁和重新构建。
最初的问题是“如何在Android中将对象从一个活动传递到另一个活动”,但没有人回答这个问题。当然,您可以序列化(Serializable、Parcelable、to/from JSON)并传递对象数据的副本,这样就可以创建具有相同数据的新对象;但它将不具有相同的引用/句柄。此外,许多其他人提到可以将引用存储在静态存储中。除非Android决定开启销毁您的活动,否则这将起作用。
因此,要真正解决原始问题,您需要一个静态查找,再加上每个对象将在重新创建时更新其引用。例如,如果调用其onCreate,则每个Android活动将重新列出自己。您还可以看到一些人如何使用任务列表按名称搜索“活动”。(系统正在临时销毁此活动实例以节省空间。getRunningTasks,任务列表实际上是每个活动的最新对象实例的专门列表)。
供参考:
停止:该活动被另一个活动完全遮挡(该活动现在位于“后台”)。已停止的活动也仍然活动(活动对象保留在内存中,它维护所有状态和成员信息,但未附加到窗口管理器)。但是,用户不再能看到它,当其他地方需要内存时,系统可能会将其关闭销毁“系统正在临时销毁此活动实例以节省空间。”
因此,消息总线是一个可行的解决方案。它基本上是“双关语”。而不是试图引用对象;然后重新构建设计,使用MessagePassing而不是SequentialCode。调试难度成倍增加;但它让你忽略了这些对操作环境的理解。实际上,每个对象方法访问都是反向的,因此调用者发布一条消息,而对象本身定义了该消息的处理程序。更多的代码,但可以使其在Android OE限制下更加健壮。
如果你想要的只是顶部的“活动”(由于到处都需要“上下文”,所以在Android应用程序中很常见),那么只要调用onResume,你就可以让每个“活动”在静态全局空间中将自己列为“顶部”。然后,AlertDialog或任何需要上下文的东西都可以从那里获取它。此外,使用全局有点讨厌,但可以简化到处上下传递上下文,当然,当您使用MessageBus时,IT无论如何都是全局的。
我使用parcelable将数据从一个活动发送到另一个活动。这是我的代码,在我的项目中运行良好。
public class Channel implements Serializable, Parcelable {
/** */
private static final long serialVersionUID = 4861597073026532544L;
private String cid;
private String uniqueID;
private String name;
private String logo;
private String thumb;
/**
* @return The cid
*/
public String getCid() {
return cid;
}
/**
* @param cid
* The cid to set
*/
public void setCid(String cid) {
this.cid = cid;
}
/**
* @return The uniqueID
*/
public String getUniqueID() {
return uniqueID;
}
/**
* @param uniqueID
* The uniqueID to set
*/
public void setUniqueID(String uniqueID) {
this.uniqueID = uniqueID;
}
/**
* @return The name
*/
public String getName() {
return name;
}
/**
* @param name
* The name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the logo
*/
public String getLogo() {
return logo;
}
/**
* @param logo
* The logo to set
*/
public void setLogo(String logo) {
this.logo = logo;
}
/**
* @return the thumb
*/
public String getThumb() {
return thumb;
}
/**
* @param thumb
* The thumb to set
*/
public void setThumb(String thumb) {
this.thumb = thumb;
}
public Channel(Parcel in) {
super();
readFromParcel(in);
}
public static final Parcelable.Creator<Channel> CREATOR = new Parcelable.Creator<Channel>() {
public Channel createFromParcel(Parcel in) {
return new Channel(in);
}
public Channel[] newArray(int size) {
return new Channel[size];
}
};
public void readFromParcel(Parcel in) {
String[] result = new String[5];
in.readStringArray(result);
this.cid = result[0];
this.uniqueID = result[1];
this.name = result[2];
this.logo = result[3];
this.thumb = result[4];
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[] { this.cid, this.uniqueID,
this.name, this.logo, this.thumb});
}
}
在活动A中,如下所示:
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("channel",(ArrayList<Channel>) channels);
Intent intent = new Intent(ActivityA.this,ActivityB.class);
intent.putExtras(bundle);
startActivity(intent);
在ActivityB中,使用如下方法获取数据:
Bundle getBundle = this.getIntent().getExtras();
List<Channel> channelsList = getBundle.getParcelableArrayList("channel");
一种选择是让自定义类实现Serializable接口,然后可以使用intent#putExtra()方法的putExtra变量(Serializable..)在intent extra中传递对象实例。
实际代码:
在自定义模型/对象类中:
public class YourClass implements Serializable {
在使用自定义模型/类的其他类中:
//To pass:
intent.putExtra("KEY_NAME", myObject);
myObject的类型为“YourClass”。然后,要从另一个活动中检索,请使用getSerializableExtra使用相同的Key名称获取对象。需要对YourClass进行类型转换:
// To retrieve object in second Activity
myObject = (YourClass) getIntent().getSerializableExtra("KEY_NAME");
注意:确保主自定义类的每个嵌套类都实现了Serializable接口,以避免任何序列化异常。例如:
class MainClass implements Serializable {
public MainClass() {}
public static class ChildClass implements Serializable {
public ChildClass() {}
}
}