Intent可以用用于跳转到别的Activity,此外也可以使用putExtra()方法在这个过程中以一个键值对(String string, 某种数据类型)的方式传递数据。
结合着Bundle,可以把数据打包在Bundle对象里面,使用putInt(key,value)和getInt(“key”)方法分别获得Bundle对象里面的数据,
和Intent自身的getXXXExtas方法也没什么区别好像。
Intent是一次性的!!!
onActivityResult()这个方法继承自Activity这个类。具体范例见这一段:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == ACTIVITY_EDIT){//标红的部分是系统预设的常量
if(resultCode==RESULT_OK){
Bundle bundle = data.getExtras();
int resultAge = bundle.getInt("Age");
String s ="姓名:David"+"\n\r"+"年龄"+resultAge;
age.setText(s);
}
if(resultCode==RESULT_CANCELED){
}
}
}
//下面这个变量估计是为了避免和R.id.XX的int类型搞混淆才设置成这样的吧
private static final int ACTIVITY_EDIT = 1;
在第二个Activity中要在相应的部分加上这么一段话:
setResult(RESULT_OK,intent);这样就带着一个RESULT_OK的标识符返回第一个Activity,也就实现了第一个Activity的restart(),当然我不清楚是不是真的调用了onRestart()或者是onResume()
下面是代码部分,没什么技术含量,直接粘贴到Eclipse里面可用
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.Harris.intent4.Activity4" >
<TextView
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="显示消息"
/>
<Button
android:id="@+id/btnPage2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="目前实际年龄"
/>
</LinearLayout>
page2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.Harris.intent4.Activity4" >
<TextView
android:id="@+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="姓名"
/>
<TextView
android:id="@+id/txtAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请输入目前实际年龄"
/>
<EditText
android:id="@+id/edtAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="在这里输入年龄"
/>
<Button
android:id="@+id/btnSure"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="确定"
/>
<Button
android:id="@+id/btnCacel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消"
/>
</LinearLayout>
Java代码部分
Activity4.java
package com.Harris.intent4;
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.TextView;
public class Activity4 extends Activity {
;
private TextView age;
private Button btnPage2;
int resultAge;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
age = (TextView) findViewById(R.id.age);
btnPage2 = (Button) findViewById(R.id.btnPage2);
btnPage2.setOnClickListener(new BtnListener());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == ACTIVITY_EDIT){
if(resultCode==RESULT_OK){
Bundle bundle = data.getExtras();
int resultAge = bundle.getInt("Age");
String s ="姓名:David"+"\n\r"+"年龄"+resultAge;
age.setText(s);
}
if(resultCode==RESULT_CANCELED){
}
}
}
private static final int ACTIVITY_EDIT = 1;
private class BtnListener implements OnClickListener{
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(Activity4.this, Second.class);
Bundle bundle = new Bundle();
bundle.putInt("Age",resultAge );
intent.putExtras(bundle);
startActivityForResult(intent,ACTIVITY_EDIT);
}
}
}
Second.java
package com.Harris.intent4;
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;
import android.widget.TextView;
public class Second extends Activity{
private TextView txtAge;
private TextView txtName;
private EditText edtAge;
private Button btnSure;
private Button btnCancel;
int resultAge;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.page2);
txtAge = (TextView) findViewById(R.id.txtAge);
txtName = (TextView) findViewById(R.id.txtName);
edtAge = (EditText) findViewById(R.id.edtAge);
btnSure = (Button) findViewById(R.id.btnSure);
btnCancel = (Button)findViewById(R.id.btnCacel);
btnSure.setOnClickListener(new Btnlistener());
btnCancel.setOnClickListener(new Btnlistener());
Intent intent =this.getIntent();
Bundle bundle=intent.getExtras();
int resultAge = bundle.getInt("Age");
edtAge.setText(""+resultAge);
}
private class Btnlistener implements OnClickListener{
@Override
public void onClick(View v) {
if(v.getId()==R.id.btnSure){
Intent intent = new Intent();
resultAge = Integer.parseInt(edtAge.getText().toString());
Bundle bundle = new Bundle();
bundle.putInt("Age", resultAge);
intent.putExtras(bundle);
setResult(RESULT_OK,intent);
finish();
}else if(v.getId()==R.id.btnCacel){
setResult(RESULT_CANCELED);
//直接调用finish()干掉这个Second的Activity就返回到第一个Activity了。
finish();
}
}
}
}
requestCode只用于标识被启动的activity,A到B用了一个startActivityForResult(intent, requestCode);
B到A又新生成了一个Intent,写上一句setResult(RESULT_OK, intent);就可以了
至于requestCode,用在初始的Activity里面,在onActivityResult(int requestCode, int ResultCode, Intent intent);里面使用
总的来说,intent只是一次性的,A用来启动B(同时传递一些数据,附上标识符requestCode);B用来启动A(同时传递一些数据,setResult(RESULT_OK)),
requestCode只是在A的onActivityResult方法里面进行判断是哪个Activity启动了A。