2010年2月12日星期五

startActivityForResult 和 onActivityResult

上兩篇文章"活動(activity)和意圖(intent)"和"包裹(Bundle)"使用startActivity(Intent)啟動另一項新活動, 原本的活動不會收到任何有關新活動退出時的信息. 這篇文章使用startActivityForResult(Intent, int)啟動新活動, 並重寫onActivityResult(int, int, Intent)方法. 當該新活動退出時, 會回調onActivityResult(int, int, Intent)方法.



修改主佈局(main.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your Name?"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/inamefield"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Launch Activity2"
android:id="@+id/launchbutton"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id = "@+id/phonenumber"
android:text=""
/>
</LinearLayout>


修改主活動代碼:
package com.AndroidIntent;

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

public class AndroidIntent extends Activity {

int REQUEST_CODE = 0;
TextView phoneNumber;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button launchButton = (Button)findViewById(R.id.launchbutton);
final EditText iNameField = (EditText)findViewById(R.id.inamefield);
phoneNumber = (TextView)findViewById(R.id.phonenumber);
launchButton.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent newIntent = new Intent(AndroidIntent.this, Activity2.class);
newIntent.putExtra("name", iNameField.getText().toString());

startActivityForResult(newIntent, REQUEST_CODE);
}});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == REQUEST_CODE){
if(resultCode==RESULT_OK){
Bundle extras = data.getExtras();
if (extras != null){
phoneNumber.setText("Phone #: " + extras.getString("phonenumber"));
}
}
}
}
}


修改layout2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="It's Activity2"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/onamefield"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="what's your phone number?"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/phonenumberfield"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="OK"
android:id="@+id/okbutton"
/>
</LinearLayout>


修改Activity2代碼:
package com.AndroidIntent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class Activity2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
TextView oNameField = (TextView)findViewById(R.id.onamefield);
final EditText phoneNumber = (EditText)findViewById(R.id.phonenumberfield);
Button okButton = (Button)findViewById(R.id.okbutton);

Bundle extras = getIntent().getExtras();
oNameField.setText("Hello " + extras.getString("name"));

okButton.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle bundle = new Bundle();
bundle.putString("phonenumber", phoneNumber.getText().toString());
Intent intent = new Intent();
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}});
}
}
記住, 需要修改清單文件(AndroidManifest.xml)加入ctivity2, 參考"活動(activity)和意圖(intent)".

2 則留言: