笔记(活动(activity)之间的通信)

1.向下一个活动传递数据。

在此就先简单介绍一下Intent:
An intent is an abstract description of an operation to be performed. It can be used withstartActivity to launch an Activity, broadcastIntent to send it to any interestedBroadcastReceiver components, andContext.startService(android.content.Intent) orContext.bindService(android.content.Intent, android.content.ServiceConnection, int) to communicate with a backgroundService

意思是:该Intent类是在android四大组件之间传递数据的信使。

Intent(Context packageContext,Class<?> cls)
          Create an intent for a specific component.
Intent putExtra(String name, String value)
          Add extended data to the intent.


Add extended data to the intent. The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".
String getStringExtra(String name)Retrieve extended data from the intent. 

参数:name - The name of the desired item. 返回:the value of an item that previously added with putExtra() or null if no String value was found.

例子:
@1通过Intent向下一个活动发送数据。
String data = "nihao";                                                                      Intent intent = new Intent(activity1.this,activity2.class);
intent.putExtra("send_data",data);
startActivity(intent);
@2通过intent接收来自上一个活动的数据。
Intent intent = getIntent();
String data = intent.getStringExtra("send_data");
接着介绍一下Bundle:

A mapping from String values to various Parcelable types.

Bundle()
          Constructs a new, empty Bundle.

putInt(String key, int value)
          Inserts an int value into the mapping of this Bundle, replacing any existing value for the given key.

Bundle bundle = new Bundle();

bundle.putInt("operate",1);

Intent intent = new Intent();
intent.putExtra(bundle);
这样的程序可以根据键值找到数值1,从而进行其他操作。
这只是简单的介绍一下Bundle,还有很多方法用来操作。


2.返回数据给上一个活动。


setResult(int resultCode, Intent data) 
          Call this to set the result that your activity will return to its caller.
参数:
resultCode - The result code to propagate back to the originating activity, often RESULT_CANCELED or RESULT_OK
data - The data to propagate back to the originating activity.    数据传播回caller activity.
Call this to set the result that your activity will return to its caller.

意思是:调用此函数将会把结果回调回caller activity.


3.开启当前活动的onActivityResult(int requestCode, int resultCode, Intent data)方法。

startActivityForResult(Intent intent, int requestCode)
          Launch an activity for which you would like a result when it finished.
参数:
     intent - The intent to start.
     requestCode - If >= 0, this code will be returned in onActivityResult() when the activity exits. 
Launch an activity for which you would like a result when it finished. When this activity exits, your onActivityResult() method will be called with the given requestCode. Using a negative requestCode is the same as callingstartActivity(android.content.Intent) (the activity is not launched as a sub-activity). 

意思是:需要一个活动结束时返回结果。当前活动退出,onActivityResult()方法将会被调用,如果requestCode < 0 时,startActivityForResult()与startActivity()效果是一样的。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值