【问题标题】:Send custom object and a string to other activity将自定义对象和字符串发送到其他活动
【发布时间】:2013-04-21 07:24:56
【问题描述】:

我需要向其他活动发送一个自定义对象和一个字符串。自定义对象(用户)实现了 Parcelable 接口。 我喜欢这样:

Intent intent = new Intent("action") ;
Bundle data = new Bundle() ;
data.putString("EDIT", "EDIT");
data.putParcelable("DATA", user ) ; //user is custom object
intent.putExtra("BUNDLE", data) ;
startActivity(intent) ;

在其他活动中,我收到了意图,但 EDIT 地图数据为空,只有 Intent 中的 Pacelable 数据,缺少 EDIT 字符串。

Intent intent  = getIntent() ;
String edit = bundle.getString("EDIT") ;  // edit = null should be EDIT
Bundle bundle =intent.getBundleExtra("BUNDLE") ;        

其他开发者知道这个问题,你能给我建议吗?谢谢。

【问题讨论】:

    标签: android


    【解决方案1】:

    老兄这样用

     Intent intent = new Intent(getBaseContext(), NextActivity.class);
     Foo foo = new Foo();
     intent.putExtra("foo ", foo);
     intent.putExtra("string", "hi");
     startActivity(intent);
    

    获取

     Foo foo = (Foo) getIntent().getParcelableExtra("foo");
     String mString =  getIntent().getStringExtra("string");
    

    【讨论】:

    • Serializable 是一个不错的选择,但官方表示,Parcelable 是满足这些需求的解决方案developer.android.com/reference/android/os/Parcelable.html
    • 嗨,Rstar,一个字符串也应该使用 Foo 发送到目标活动。你有什么其他的建议?这个!
    • 非常感谢!我改变了从包中获取的顺序。它工作正常! codeBundle bundle =intent.getBundleExtra("BUNDLE") ;字符串编辑 = bundle.getString("EDIT") ; // edit = null 应该是 EDIT code
    • 这个问题肯定和开发环境有关。我运行旧代码,现在可以了吗。
    猜你喜欢
    • 1970-01-01
    • 2014-12-05
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多