你知道吗?关于android意图

时间:2014-10-06 07:58:29

标签: android android-intent onactivityresult

我的源代码如下:

MainActivity中的

protected void onActivityResult(int requestCode, int resultCode, Intent data){
    super.onActivityResult(requestCode, resultCode, data);

    switch(requestCode)
    {
    case LOGIN_ACTIVITY_REQUEST_CODE:
    {
        if(resultCode == RESULT_OK){
            Bundle bd = new Bundle();
            bd = data.getBundleExtra("bundle");
        }
    }
    default:
    {
        Log.d(DEBUG_ACTIVITY_CLASS_NAME, "ERROR : onActivityResult - unknown activity code");
    }
    }
}

第二个活动的代码是:

public void returnToGameActivity(Bundle bundle) {                                       
        Intent intent = new Intent();
        intent.putExtra("bundle", bundle);
        this.setResult(RESULT_OK, intent);

        finish();
    }

但我无法在MainActivity中的onActivityResult()中接收Bundle类型的数据'bd'。为什么呢?

但在这种情况下,我可以获得bd:

的数据

在第二项活动中,

putExtra("string", "test string");

在MainActivity中,

String str = getStringExtra("string");

为什么我无法以捆绑类型获取数据?

1 个答案:

答案 0 :(得分:0)

要将Bundle元素从一个活动转移到另一个活动,请使用以下代码:

Intent i=new Intent(First.this,Second.class);
        //i.putExtra("mylist",amt);
         Bundle b = new Bundle();
         b.putSerializable("bundleinterest", (Serializable) amtint);
         b.putSerializable("bundleobj", (Serializable) amt);
            i.putExtras(b);
        startActivity(i);

在Second.class中:

        Bundle bn = new Bundle();
         bn = getIntent().getExtras();
         getobj = new ArrayList<CompoundAmount>();
         getinterestobj=new ArrayList<CompoundIntAmount>();
         getobj = (ArrayList<CompoundAmount>) bn.getSerializable("bundleobj");
         getinterestobj = (ArrayList<CompoundIntAmount>) bn.getSerializable("bundleinterest");

说明:

传输Bundles时,

使用Serializable或Parcelable 接口。 您的set-get类必须将Serializable实现为:

public class CompoundIntAmount implements Serializable {

     private final String amt;



     public CompoundIntAmount(String amt){
         this.amt = amt;

     }

     public String getIntAmount() {
         return amt;
     }

}

这里的CompoundAmount和CompoundIntAmount是自定义set-get类,必须实现Serializable(如上所述)。