我如何通过一个自定义类型的对象从一个活动到另一个使用类意图的putExtra()方法?
当前回答
如果你有一个单例类(fx Service)作为你的模型层的网关,它可以通过在该类中有一个带有getter和setter的变量来解决。
活动一:
Intent intent = new Intent(getApplicationContext(), Activity2.class);
service.setSavedOrder(order);
startActivity(intent);
活动二:
private Service service;
private Order order;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quality);
service = Service.getInstance();
order = service.getSavedOrder();
service.setSavedOrder(null) //If you don't want to save it for the entire session of the app.
}
在服务:
private static Service instance;
private Service()
{
//Constructor content
}
public static Service getInstance()
{
if(instance == null)
{
instance = new Service();
}
return instance;
}
private Order savedOrder;
public Order getSavedOrder()
{
return savedOrder;
}
public void setSavedOrder(Order order)
{
this.savedOrder = order;
}
此解决方案不需要对相关对象进行任何序列化或其他“打包”。但是,只有在使用这种架构时才会有好处。
其他回答
我使用Gson及其强大而简单的api在活动之间发送对象,
例子
// This is the object to be sent, can be any object
public class AndroidPacket {
public String CustomerName;
//constructor
public AndroidPacket(String cName){
CustomerName = cName;
}
// other fields ....
// You can add those functions as LiveTemplate !
public String toJson() {
Gson gson = new Gson();
return gson.toJson(this);
}
public static AndroidPacket fromJson(String json) {
Gson gson = new Gson();
return gson.fromJson(json, AndroidPacket.class);
}
}
2个函数,你将它们添加到你想要发送的对象中
使用
将对象从A发送到B
// Convert the object to string using Gson
AndroidPacket androidPacket = new AndroidPacket("Ahmad");
String objAsJson = androidPacket.toJson();
Intent intent = new Intent(A.this, B.class);
intent.putExtra("my_obj", objAsJson);
startActivity(intent);
在B接收
@Override
protected void onCreate(Bundle savedInstanceState) {
Bundle bundle = getIntent().getExtras();
String objAsJson = bundle.getString("my_obj");
AndroidPacket androidPacket = AndroidPacket.fromJson(objAsJson);
// Here you can use your Object
Log.d("Gson", androidPacket.CustomerName);
}
我几乎在我做的每个项目中都使用它,我没有任何性能问题。
我知道有点晚了,但很简单。你所要做的就是让你的类实现Serializable
public class MyClass implements Serializable{
}
然后你可以传递给一个意图,比如
Intent intent=......
MyClass obje=new MyClass();
intent.putExtra("someStringHere",obje);
要得到它,你只需打电话
MyClass objec=(MyClass)intent.getExtra("theString");
最简单的方法是在item是字符串的情况下使用以下代码:
intent.putextra("selected_item",item)
接收:
String name = data.getStringExtra("selected_item");
Intent i = new Intent();
i.putExtra("name_of_extra", myParcelableObject);
startACtivity(i);
您需要将对象序列化为某种字符串表示形式。一种可能的字符串表示是JSON,如果你问我,在android中序列化JSON最简单的方法之一是通过谷歌GSON。
在这种情况下,你只需要输入字符串返回值from (new Gson()).toJson(myObject);并检索字符串值并使用fromJson将其转换回您的对象。
但是,如果您的对象不是非常复杂,那么可能不值得这样做,您可以考虑传递对象的单独值。
推荐文章
- apk (.apk)和应用程序包(.aab)的区别
- 如何设置超时在改造库?
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- TextView的字体大小在Android应用程序改变字体大小从本机设置
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的