简体   繁体   English

Android:intent.putExtra()会更改对象吗?

[英]Android: intent.putExtra() changes objects?

I want to pass myObject to the NewActivity class. 我想将myObject传递给NewActivity类。 For this I use the putExtra() and getExtras() methods from Intent . 为此,我使用putExtra()getExtras()意图的方法。 It appears however that the object changes somehow? 但是看来对象发生了什么变化? I'm printing the object both before and after I pass it. 我在传递对象之前和之后都在打印该对象。

Is this normal? 这正常吗? I'm running into problems because I'm using myObject in a HashMap, and since the object in NewActivity is essentially an entirely different object I can't use it as a key anymore. 我遇到了问题,因为我在HashMap中使用myObject,并且由于NewActivity中的对象本质上是一个完全不同的对象,因此我不能再将其用作键。

Passing the object: 传递对象:

Intent myIntent = new Intent(context, NewActivity.class);
MyClass myObject = new MyClass();
myIntent.putExtra("test", myObject);
System.out.println("OBJECT BEFORE: " + myObject);

Retrieving the object: 检索对象:

MyClass myObject = (MyClass) getIntent().getExtras().getSerializable("test");
System.out.println("OBJECT AFTER: " + myObject);

The (different) prints: (不同)打印:

I/System.out: OBJECT BEFORE: my.project.MyClass@8fe5ae4
I/System.out: OBJECT AFTER: my.project.MyClass@9f1fb7b

change this 改变这个

getIntent().getExtras().getSerializable() getIntent()。getExtras()。getSerializable()

to

getIntent().getSerializableExtra("test")

Serializing and de-serializing does create new objects. 序列化和反序列化确实会创建新对象。

If that's an issue for you because you use it as a key into a HashMap , you should implement the boolean equals(Object other) and the int hashCode() methods in your class. 如果这是您的问题,因为您将其用作HashMap的键,则应在类中实现boolean equals(Object other)int hashCode()方法。

Check out Java hashCode() on Wikipedia and public int hashCode() in Oracle's documentation to learn how to implement a "hashable" object. 查看Wikipedia上的Java hashCode()和Oracle文档中的public int hashCode() ,以了解如何实现“可哈希”对象。

If your object doesn't depend on identity anymore when being used as a key your problem should be solved. 如果您的对象在用作密钥时不再依赖于身份,则应该解决问题。

That is how Serializable objects work. 这就是可序列化对象的工作方式。 100% basic Java. 100%基本的Java。

They serialize objects into raw data, and then when they're un-serialized, it will create a completely new object of the same type, and set all the values that the old object had onto the new one. 他们将对象序列化为原始数据,然后在不进行序列化时将创建一个相同类型的全新对象,并将旧对象具有的所有值设置为新对象。

So the object references won't be the same, which is also why a different hashCode is printed out when you call System.out.println . 因此,对象引用将不相同,这也是为什么当您调用System.out.println时会打印出不同的hashCode的原因。

The internal values of the object will be the same. 对象的内部值将相同。

For passing object one activity to another, Your object class should implement "Serializable". 为了将一个对象的活动传递给另一个活动,您的对象类应实现“可序列化”。

public class MyClass implements Serializable{
// 
}

pass data 传递数据

Intent myIntent = new Intent(context, NewActivity.class);
MyClass myObject = new MyClass();
myIntent.putExtra("test", myObject);

Retrieve data 检索数据

MyClass myObject = (MyClass) getIntent().getSerializable("test");

Hope this will work :) 希望这会工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM