Android intent数据传递

1. 主叫Activity向被叫Activity传递数据:

(1)主叫Activity写数据:

Intent intent = new Intent(action);
intent.putExtra("key", "value");
startActivity(intent);

   

(2)被叫Activity读数据:

 

Intent intent = getIntent();  
String strFromCaller = intent.getStringExtra("key");  

 

2. 被叫Activity向主叫Activity放回数据:

(1)主叫Activity以startActivityForResult()方式启动被叫Activity:

 

Intent intent = new Intent();  
intent.setClass(getApplicationContext(), NewAcitvity.class);  
// START_NEWACTIVITY is a predefined constant to identify the Activity invoked by this intent.  
startActivityForResult(intent, START_NEWACTIVITY);  

 

 

(2)被叫Activity写数据:

 

Uri uri = Uri.parse("back info");  
Intent data = new Intent(null, uri);  
data.putExtra("key", "value");  
setResult(Activity.RESULT_OK, data);  

 

 

(3)主叫Activity读数据:

 

@Override  
protected void onActivityResult(int requestCode, int resultCode, Intent data)  
{  
    super.onActivityResult(requestCode, resultCode, data);  
    switch (requestCode)  
    {  
        case START_NEWACTIVITY:  
            if (resultCode == Activity.RESULT_OK)  
            {  
                Toast.makeText(this, data.getStringExtra("key"), Toast.LENGTH_LONG).show();  
//              Toast.makeText(this, data.getData().toString(), Toast.LENGTH_LONG).show();  
            }  
            break;  
    }  
}  

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值