概述:java
若是啓動另外一個Activity,而且但願返回結果給當前的Activity,那麼可以使用startActivityForResult()方法,在這種狀況下,也是經過Bundle來進行數據交換的。android
爲了獲取到被啓動的Activity,當前的Activity要重寫onActivityResult(int requestCode, int resultCode, Intent intent)方法,其中參數1表明請求碼,參數2表明返回的結果碼,由開發者根據業務須要自定義。app
一個Activity中可能包含多個按鈕,並調用多個startActivityForResult()方法來打開多個不一樣的Activity處理不一樣的業務。當這些新的Activity關閉後,系統都會回調前面那個Activity的onActivityResult()方法。爲了知道該方法是由哪一個請求的結果所觸發的,可利用requestCode請求碼;爲了知道返回的數據來自於哪一個新的Activity,可利用resultCode結果碼。ide
1、項目實例:this
在第二個Activity裏讓用戶選擇所在地,並將選擇的結果返回給第一個Activityspa
完整代碼:code
ForResultActivity.javaxml
package com.xsjayz.ac; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class ForResultActivity extends Activity { private Button button; private EditText cityEdit; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.btn); cityEdit = (EditText) findViewById(R.id.city_edit); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(ForResultActivity.this, SecondActivity.class); // 啓動對應的Activity,並等待返回的結果,其中0爲請求碼(requestCode),用於標識該請求。 startActivityForResult(intent, 0); } }); } /** * 重寫onActivityResult()方法:以回調的方式來獲取指定Activity返回的結果。 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { // 當requestCode和resultCode都爲0時(處理特定的結果)。 if (requestCode == 0 && resultCode == 0) { Bundle bundleData = intent.getExtras(); String resultCity = bundleData.getString("city"); cityEdit.setText(resultCity); } } }
SecondActivity.java對象
package com.xsjayz.ac; import android.app.ExpandableListActivity; import android.content.Intent; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.ExpandableListAdapter; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class SecondActivity extends ExpandableListActivity { private String[] provinces = new String[] { "上海", "江西", "浙江" }; private String[][] cities = new String[][] { { "徐匯", "黃浦", "浦東" }, { "上饒", "萍鄉", "南昌" }, { "嘉興", "紹興", "杭州" } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /** * ExpandableListAdapter */ ExpandableListAdapter adapter = new BaseExpandableListAdapter() { @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } @Override public boolean hasStableIds() { return true; } // 該方法決定每一個組選項的外觀 @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { LinearLayout linearLayout = new LinearLayout(SecondActivity.this); linearLayout.setOrientation(0); ImageView logo = new ImageView(SecondActivity.this); linearLayout.addView(logo); TextView textView = getTextView(); textView.setText(getGroup(groupPosition).toString()); linearLayout.addView(textView); return linearLayout; } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public int getGroupCount() { return provinces.length; } // 獲取指定組位置處的組數據 @Override public Object getGroup(int groupPosition) { return provinces[groupPosition]; } @Override public int getChildrenCount(int groupPosition) { return cities[groupPosition].length; } // 該方法決定每一個子選項的外觀 @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { TextView textView = getTextView(); textView.setText(getChild(groupPosition, childPosition).toString()); return textView; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } // 獲取指定組位置、指定子列表項處的子列表項數據 @Override public Object getChild(int groupPosition, int childPosition) { return cities[groupPosition][childPosition]; } private TextView getTextView() { AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 64); TextView textView = new TextView(SecondActivity.this); textView.setLayoutParams(lp); textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); textView.setPadding(60, 0, 0, 0); textView.setTextSize(20); return textView; } }; // 設置該窗口顯示列表 setListAdapter(adapter); getExpandableListView().setOnChildClickListener( new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { // 獲取啓動該Activity的上一個Activity對應的Intent Intent intent = getIntent(); Bundle data = new Bundle(); data.putString("city", cities[groupPosition][childPosition]); intent.putExtras(data); // 設置SecondActivity的結果碼(resultCode),並設置在當前結束後退回去的Activity setResult(0, intent); finish(); return false; } }); } }
main.xmlutf-8
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/set_city_btn" /> <EditText android:id="@+id/city_edit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:editable="false" android:hint="@string/city_edit" /> </LinearLayout>
2、問題
今天在寫這段代碼的時候,遇到一個納悶的問題:
一、從A跳轉到B
// 從AActivity跳轉到BActivity,並傳遞參數過去 Intent intent = new Intent(AActivity.this,BActivity.class); intent.putExtra("path", filterImgPath); startActivityForResult(intent,REQUEST_CODE_GETIMAGE_FILTER);
二、在B處接收從A傳遞的數據,沒有問題
getIntent().getStringExtra("path");
三、在B處理完成後,返回到A的過程,觸發A的onActivityResult()方法
// 完成 doneButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = getIntent(); // 情形1 // Bundle bundle = new Bundle(); // bundle.putString("filterPath", mNewPath); // 情形2 intent.putExtra("filterPath", mNewPath); ImageFilterActivity.this.setResult(RESULT_OK, intent); ImageFilterActivity.this.finish(); } });
四、在A處的onActivityResult()裏
else if (requestCode == REQUEST_CODE_GETIMAGE_FILTER) { // 情形1,得到的bundle 不爲null,可是newPath爲null // Bundle bundle = data.getExtras(); // String newPath = bundle.getString("filterPath"); // 情形2,newPath不爲null String newPath = data.getStringExtra("filterPath"); }
五、問題應該處在3,即3裏面採用new一個Bundle對象來傳遞數據,這個Bundle應該沒有和Intent對象關聯,致使的結果就是在4裏得到的Bundle對象不是3裏的Bundle對象。