標籤:
1、向下一個活動傳遞資料
Intent中提供了一系列的putExtra()方法的重載,可以把我們想要傳的資料暫存在Intent中,啟動一個活動後,只需把這些資料在從Intent中,取出來即可。做一個簡單的Demo示範一下:
建立項目MyDataTrans,同時在項目中建立類Second.java和布局second.xml。
在activity_main.xml中修改布局(TextView用於擷取從上一活動或許的值):
<LinearLayout android:id="@+id/lin1" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Click On"/> </LinearLayout> <LinearLayout android:id="@+id/line2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/lin1" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
activity_main.xml
在second.xml中修改布局(TextView用於擷取從上一活動或許的值):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content"/> <Button android:id="@+id/btn2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Back"/></LinearLayout>
second.xml
在MainActivity.java中添加按鈕1的單擊事件,跳轉到活動2中,
btn1=(Button)findViewById(R.id.btn); btn1.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent1=new Intent(MainActivity.this,Second.class); intent1.putExtra("mydata", "你好,我來自介面1。。。。"); startActivity(intent1); } });setOnClickListener()
這裡我們用顯示Intent的方式啟動Second,並通過putExtra()方法傳遞一個字串。其中putExtra()接受兩個參數,第一個參數是鍵,用於後面從Intent中取值,第二個參數就是要傳遞的值。
接著進入到Second.java中,在這裡我們的目的就是要把上一個頁面傳的值進行取出顯示,onCreat()方法中添加如下代碼:
protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.second); btn2=(Button)findViewById(R.id.btn2); textView2=(TextView)findViewById(R.id.textView2); //擷取上一個活動傳出的值 Intent intent=getIntent(); textView2.setText(intent.getStringExtra("mydata"));}onCreat()
通過getIntent()方法擷取用於啟動的Second的Intent,然後調用getStringExtra()方法,擷取相應的索引值,就可以得到傳遞的資料了。然後把傳遞的資料在TextView上顯示出來。
2、返回資料給上一個活動
方法其實和上面的類似,在MainActivity.java中 依舊是點擊Button控制項跳轉到Second頁面,
Intent intent1=new Intent(MainActivity.this,Second.class); startActivityForResult(intent1, 1);
這裡我們用startActivityForResult()方法來啟動Second,startActivityForResult()方法接受兩個參數,第一個參數是Intent,第二個參數是請求碼,用於在回調中判斷資料的來源。其中,請求碼只要是唯一值就可以了。
在Second中給按鈕註冊點擊事件,並添加返回資料的邏輯:
btn2=(Button)findViewById(R.id.btn2);btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intentback=new Intent(); intentback.putExtra("data_back", "介面1,你好!!!"); setResult(RESULT_OK, intentback); finish(); } });View Code
這裡構建Intent僅僅用於傳遞資料而已,沒有任何指定的意圖。緊接著要傳遞的資料放在intentback中,然後調用setResult()方法,用於向上一個活動返回資料。setResult()方法接受兩個參數,第一個參數用於向上一個活動返回處理結果,一般只有RESULT_OK或者RESULT_CANCELED這兩個值,第二個參數則是把帶有資料的intentback傳遞迴去,然後調用finish()方法來銷毀當前活動(必須銷毀,否則無法回調)。
由於使用startActivityForResult()方法啟動的Second,所以在被銷毀之後會回調到上一個活動的onActivityResult()方法,因此需要在MainActivity.java中重新寫這個方法來得到返回的資料,如下所示:
protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); textView1=(TextView)findViewById(R.id.textView1); super.onActivityResult(requestCode, resultCode, data); switch(requestCode){ case 1: if(resultCode==RESULT_OK){ textView1.setText(data.getStringExtra("data_back")+"我來自介面2!!!"); } break; default: } }onActivityResult()
onActivityResult()有三個參數,第一個參數requestCode,即我們在啟動活動時傳入的請求碼,第二個參數resultCode,即我們在返回出具時傳入的處理結果,第三個參數data即我們帶有返回資料的Intent(這裡指intentback)。
由於一個活動中有可能調用startActivityForResult()方法去啟動很多不同的活動,但是每一個活動返回的資料都會調到onActivityResult()中,所以首先要做的就是根據requestCode,即請求碼來檢查判斷資料來源。然後再根據resultCode的值來判斷處理結果是否成功。最後有打他來傳入返回的資料。
答疑:當使用者不是通過按鈕的單擊事件返回,而通過back鍵返回時是不是就沒法擷取想要返回的資料了呢?
這時候只需我們調用一個叫做onBackPressed()的方法來解決這個問題。代碼如下:
public void onBackPressed() { Intent intentback=new Intent(); intentback.putExtra("data_back", "介面1,你好!!!"); setResult(RESULT_OK, intentback); finish(); }onBackPressed()
其實,我們可以 單獨寫一個返回資料的方法,然後在按鈕註冊點擊事件或者onBackPressed()時直接調用就好。
安卓活動間的傳值問題