[Andriod] 透過Intent轉換Activity

  • 27941
  • 0

透過Intent轉換Activity, 透過Bundle傳遞不同Intent的資料, 並透過startActivityForResult及覆寫onActivityResult在返回時保留原先的輸入內容

 res\layout\layout1.xml


<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/tvMsg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/name" />
        <EditText
            android:id="@+id/etName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10" />
        <Button
            android:id="@+id/btGo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="onClick"
            android:text="@string/go" />
    </TableRow>
</TableLayout>

 

res\layout\layout2.xml


<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:id="@+id/tvMsg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="@string/back" />
</TableLayout>

 

src\...\Activity1.java

startActivity只能單向傳遞資料, startActivityForResult可以雙向傳遞資料, 但必須複寫Activity.onActivityResult
startActivity與finish()配合使用, 當執行finish()即關閉Activity, 既使按下模擬器的返回鍵也無法回到上個Activity; 若註解finish()在按下返回鍵, android並非回到上個Activity, 而是重新產生新的Activity


package com.example.excercise3;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;

public class Activity1 extends Activity {
	
	private EditText etName;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout1);
		
		etName = (EditText)findViewById(R.id.etName);
	}

	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btGo:
			Intent intent = new Intent();
			intent.setClass(Activity1.this, Activity2.class);
			Bundle bundle = new Bundle();
			bundle.putString("name", etName.getText().toString());
			intent.putExtras(bundle);
			startActivityForResult(intent,0);
			//startActivity(intent);
			//Activity1.this.finish();
			break;	
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data){
		switch(resultCode){
		case RESULT_OK:
			Bundle bundle = data.getExtras();
			etName.setText(bundle.getString("name"));
			break;
		default:
			break;
		
		}
	}

}

 

src\...\Activity2.java


package com.example.excercise3;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Activity2 extends Activity{
	
	private TextView tvMsg;
	private Intent intent; 
	private Bundle bundle;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout2);
		
		intent = this.getIntent();
		bundle = intent.getExtras();
		tvMsg = (TextView)findViewById(R.id.tvMsg);
		tvMsg.setText("Hi "+bundle.getString("name")+"!!!");
		
	}

	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btBack:
			Activity2.this.setResult(RESULT_OK,intent);
			Activity2.this.finish();
			break;	
		}
	}

}

 

AndroidManifest.xml

必須要把新的activity加入application, 否則會錯誤, 此外, android會先執行有定義android.intent.action.MAIN及android.intent.category.LAUNCHER的activity 


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.excercise3"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.excercise3.Activity1"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="Activity2"></activity>
    </application>

</manifest>