Factivity
package com.example.wuzuo.app2;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by wuzuo on 2016/7/21.
*/
public class FActivity extends Activity {//创建一个显示页面
private Button button1;
private Context context;
private Button button2;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//
setContentView(R.layout.factivity);
context=this;
button1= (Button) findViewById(R.id.button1_first);
button2= (Button) findViewById(R.id.button2_second);
textView= (TextView) findViewById(R.id.textView1);//初始化
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {//通过点击button1来实现页面跳转,内部监听事件
Intent intent = new Intent(context, SActivity.class);//context为FActivity对象,上下文对象this,intent为意图,SActivity.class请求的对象
startActivity(intent);//让请求的意图实现页面跳转
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, SActivity.class);
startActivityForResult(intent, 1);
/*startActivityForResult
*页面跳转但是返回数据,
* intent表示跳转到哪的意图,
* request表示标识页面代码,请求的标志
*/
}
});
}
/*onActivityResult接收数据的方法
*requestCode:请求的标志
* resultCode:第二个页面返回的标志,哪个页面跳转的标识
*data:第二个页面回传的数据,data是回传一个intent对象
*
*
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==1&&resultCode==2)//通过请求码(去SActivity)和回传码(回传数据到第一个页面)判断回传的页面
{
data.getStringExtra("data");
String content=data.getStringExtra("data");//字符串content得到data数据
textView.setText(content);//textView得到字符串
}
}
}
Sactivity
package com.example.wuzuo.app2;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
/**
* Created by wuzuo on 2016/7/21.
*/
public class SActivity extends Activity {
private Button button3;
private String content="Good job!";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sactivity);
button3= (Button) findViewById(R.id.button4);//初始化
/*
回传数据实际上是回传一个intent对象
*/
button3 .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent data =new Intent();//只是回传数据就不用写跳转对象
data.putExtra("data",content);//数据放到data里面去
setResult(2,data);//返回data,2为result,data为intent对象
finish();//页面销毁
}
});
}
}
sactivity
<?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">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button4" />
</LinearLayout>
factivity
<?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:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一种启动方式"
android:id="@+id/button1_first" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二种启动方式"
android:id="@+id/button2_second" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="把第二个页面回传的数据显示出来"
android:id="@+id/textView1"
/>
</LinearLayout>