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

时间:2022-07-14 20:02:33

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_OKdata - 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()效果是一样的。