Android的序列化/对象传递和返回 [英] Android Serialization/Object Passing and Returning

查看:197
本文介绍了Android的序列化/对象传递和返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有一个应用程序,管理工作订单。在该应用中的一个组成部分,我已检索从web服务工作订单的列表。用户然后拾取从列表中工作订单中的一个和列表开始一个新的活动(传递工作单对象的话),显示了工作单的详细信息。在此期间,用户可以编辑工作单的部分。如果用户返回到列表中(通过返回按钮),我需要通过返回到工作单列表,要么更新修改工作单或替换为新修改的一个旧对象。否则,(如目前正在发生的事情),用户编辑的工作单,但如果他们回到列表并重新选择相同的工作单,工作单细节activty显示所有的旧数据。什么是做到这一点的最好办法。目前,我有工作单类实现Serializable所以工作单对象可以传递给后续活动。

So I have an app that manages workorders. In one part of the app, I have a list of workorders that has been retrieved from a web service. The user then picks one of the workorders from the list and the list starts a new activity (passing the Workorder object to it) that shows the workorder details. The user can edit parts of the workorder during this time. If the user returns to the list (via the back button), I need to pass the modified workorder BACK to the workorder list and either update or replace the old object with the new modified one. Otherwise, (as is currently happening) the user edits the workorder, but if they go back to the list and select the same workorder again, the workorder detail activty shows all the old data. What is the best way to do this. Currently I have the Workorder class implement Serializable so the workorder objects can be passed to successive activities.

所以这工作: 列表 - >工作单A

So this works: List -> Workorder A

但是,这是我遇到的问题: 名单< - 工作单A(修改)

But this is where I'm having the issue: List <- Workorder A (modified)

我不知道我是否应该使用startActivtyForResult并通过工作单对象返回与否。我知道这是有可能的,但我不知道是否有更优雅的方式来做到这一点。感谢您的任何和所有帮助,这是极大的AP preciated!

I'm not sure if I should be using startActivtyForResult and passing the workorder object back or not. I know it's a possibility, but I'm not sure if there are more graceful ways to do this. Thanks for any and all help as it is greatly appreciated!

推荐答案

如果您的工作单对象已序列化的,你可以很容易地在的bundle 。要将对象保存到一个意图,你会怎么做:

If your Workorder object is already serializable you could easily pass the object within the bundle of an intent. To save the object to an intent you would do:

intent.putExtra("SomeUniqueKey", [intance of workorder]);

和到另一个活动加载:

Workorder workorder = (Workorder) intent.getSerializableExtra("SomeUniqueKey");

如果您使用的是startActivityForResult它会去为这样的:

If you are using startActivityForResult it would go as such:

WorkorderListActivity.java:

WorkorderListActivity.java:

Intent intent = new Intent(this, WorkorderDetailsActivity.class);
intent.putExtra("SomeUniqueKey", _workorder);
startActivityForResult(intent, [UniqueNumber]);

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
     if (requestCode == [UniqueNumber])
     {
         if (resultCode == RESULT_OK)
         {
             Workorder workorder = (Workorder) intent.getSerializableExtra("SomeUniqueKey");
             // Do whatever with the updated object
         }
     }
 }

WorkorderDetailsActivity.java:

WorkorderDetailsActivity.java:

public void onCreate(Bundle savedInstanceState)
{
     Bundle bundle = getIntent().getExtras();
     _workorder = (Workorder) bundle.getSerializable("SomeUniqueKey");
     // Display the object
}

public void onBackPressed()
{
    // Update _workorder object
    Intent intent = getIntent();
    intent.putExtra("SomeUniqueKey", _workorder);
    setResult(RESULT_OK, intent);
}

我认为这应该工作。

I believe this should work.

这篇关于Android的序列化/对象传递和返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆