今天發現自己連Bundle類都沒有搞清楚,於是花時間研究了一下。
根據google官方的文檔(http://developer.android.com/reference/android/os/Bundle.html)
Bundle類是一個key-value對,“A mapping from String values to various Parcelable types.”
兩個activity之間的通訊可以通過bundle類來實現:
開始做了一個demo來模擬activity互相傳遞。
Activity1
public class Activity1 extends Activity {
Button bt_1;
EditText et_1;
TextView tv_1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pass1);
bt_1 = (Button) findViewById(R.id.bt_1);
et_1 = (EditText) findViewById(R.id.et_1);
tv_1 = (TextView) findViewById(R.id.tv_1);
try {
// 獲取返回值
Bundle bundle = this.getIntent().getExtras();
// 獲取數據內容
String userReturn = bundle.getString("userReturn");
// 顯示在TextView中
tv_1.setText("傳回:" + userReturn);
} catch (Exception e) {
System.out.println("error:" + e);
}
bt_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 創建Intent
Intent intent = new Intent();
// 升職from和to的activity
intent.setClass(Activity1.this, Activity2.class);
// 設置要傳送的數據
Bundle bundle = new Bundle();
bundle.putString("userInput", et_1.getText().toString());
// 將數據設置到Intent中
intent.putExtras(bundle);
Activity1.this.startActivity(intent);
Activity1.this.finish();
}
});
}
}
public class Activity2 extends Activity {
Button bt_2;
TextView tv_2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.pass2);
tv_2 = (TextView) findViewById(R.id.tv_2);
bt_2 = (Button) findViewById(R.id.bt_2);
Bundle bundle = this.getIntent().getExtras();
// 獲取數據
final String userInput = bundle.getString("userInput");
// 顯示在TextView中
tv_2.setText(userInput);
bt_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 創建Intent實體
Intent intent = new Intent();
intent.setClass(Activity2.this, Activity1.class);
// 創建要傳回的數據
Bundle bundle = new Bundle();
bundle.putString("userReturn", "return value " + userInput);
intent.putExtras(bundle);
Activity2.this.startActivity(intent);
Activity2.this.finish();
}
});
}
}
這個demo可以完成兩個Activity之間的互相調用,但是無法說明是誰傳遞過來的數據,有可能是別的activity傳遞過來的數據。
所以上網查了下相關文檔,添加了一下內容,主要用的是startActivityForResult來實現
Activity1中修改
<pre name="code" class="java" style="color: rgb(51, 51, 51); font-size: 14px; line-height: 26px;"><pre name="code" class="java"> //Activity1.this.startActivity(intent);
Activity1.this.startActivityForResult(intent, 123456);
//Activity1.this.finish();
Activity2中修改
<pre name="code" class="java"> //Activity2.this.startActivity(intent);
Activity2.this.setResult(123456, intent);
Activity2.this.finish();
之後需要在Activity1中重寫 onActivityResult方法替代原有的try catch
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == 123456){
Bundle bundle = data.getExtras();
String userReturn = bundle.getString("userReturn");
tv_1 = (TextView) findViewById(R.id.tv_1);
tv_1.setText("傳回:"+userReturn);
}
}