本例中MainActivity为:FirstActivity.java
FirstActivity如下:
package com.wyl.intentmultiactivitytest; 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 FirstActivity extends Activity {
//
Button btn01;
Button btn02;
EditText et; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstctivity);// 绑定页面
btn01 = (Button) findViewById(R.id.button1);
btn02 = (Button) findViewById(R.id.button2);
et = (EditText)findViewById(R.id.editText1);
btn01.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 1.上下文对象,Context
// 2.目标文件
Intent intent = new Intent(FirstActivity.this,
SecondActivity.class);
System.out.println("======kaishi :====");
startActivity(intent);// 实现跳转到第二个页面 }
});
btn02.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 1.上下文对象,Context
// 2.目标文件
Intent intent = new Intent(FirstActivity.this,
SecondActivity.class);
System.out.println("======第一个页面的第二种跳转kaishi :====");
startActivityForResult(intent, 1);;// 实现跳转到第二个页面 }
});
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1&&resultCode==2){
String plnr1 = data.getStringExtra("plnr");
String plnr2 = data.getExtras().getString("plnr");
System.out.println("==========:"+plnr1+",plnr2:"+plnr2);
et.setText("data.getStringExtra('plnr1')"+plnr1+",plnr2 = data.getExtras().getString('plnr')的方式"+plnr2);
}
}
}
SecondActivity:
package com.wyl.intentmultiactivitytest; 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.Toast; public class SecondActivity extends Activity {
Button btn01;
Button btn02;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);// 绑定页面
System.out.println("=====wo shi the second yemian =====");
btn01 = (Button) findViewById(R.id.button21);
btn02 = (Button) findViewById(R.id.button22);
et = (EditText)findViewById(R.id.editText01);
btn01.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(SecondActivity.this, FirstActivity.class);
System.out.println("==cong dier tiaodao diyiye==");
startActivity(intent);
}
});
btn02.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
String content = et.getText().toString();//获取评论的内容
intent.putExtra("plnr", content); //2是状态码,随便自己设置,用来判断是哪个页面的返回值,自己不弄混了就行
setResult(2, intent); //这是最关键的一步,
Toast.makeText(SecondActivity.this, intent.getExtras().getString("plnr"),1000);
finish();//关闭该页面,就会自动返回到了第一页了。
}
}); }
}
相关的layout文件:
firstctivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="new Intent(context,目标activity.class),startActivity(intent)方式跳转" /> <Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二种方式跳转" /> <EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="我只是个提示框框而已"> <requestFocus />
</EditText> </LinearLayout>
second_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <EditText
android:id="@+id/editText01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="录入要返回到第一页的内容" />
<Button
android:id="@+id/button21"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是第二个activity页面" />
<Button
android:id="@+id/button22"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="startActivityForResult方式返回,并且回传数据" />
</LinearLayout>
效果图如下: