Activity跳转的数据交换问题---学习笔记
以解决这两个问题为例:
问题一:有两个Activity(A和B),从A跳转到B,同时B需要A中的数据:
效果图:
代码:
MainActivity类:
package com.test.bundletest;
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.RadioButton;
public class MainActivity extends Activity {
private EditText name_edt;
private EditText age_edt;
private RadioButton sex_rbtn;
private Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String name = name_edt.getText().toString();
String age = age_edt.getText().toString();
String sex = sex_rbtn.isChecked()?"男":"女";
Student stu = new Student(name, age, sex);
Intent intent = new Intent(MainActivity.this,SecondActicity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("student", stu);
intent.putExtras(bundle);
//启动intent对应的Activity
startActivity(intent);
}
});
}
private void initView() {
name_edt = (EditText) findViewById(R.id.id_edt_name);
age_edt = (EditText) findViewById(R.id.id_edt_age);
sex_rbtn = (RadioButton) findViewById(R.id.id_sex_male);
submit = (Button) findViewById(R.id.id_btn_submit);
}
}
SecondActivity类:
package com.test.bundletest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActicity extends Activity {
private TextView tv_name;
private TextView tv_age;
private TextView tv_sex;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
initView();
//获取启动该Result的Intent
Intent intent = getIntent();
//直接通过Intent取出他所携带的Bundle数据包中的数据
Student student = (Student) intent.getSerializableExtra("student");
tv_name.setText(student.getName());
tv_age.setText(student.getAge());
tv_sex.setText(student.getSex());
}
private void initView() {
tv_name = (TextView) findViewById(R.id.id_name);
tv_age = (TextView) findViewById(R.id.id_age);
tv_sex = (TextView) findViewById(R.id.id_sex);
}
}
上边的实例中就是在MainActivity类中发送数据,然后发送到SecondActivity类,由SecondActivity处理接收到的数据并显示出来。
收获
当A携带数据发送给B的时候,然后由B接收处理数据时:
可以使用Intent中携带Bundle中的数据的方式来处理。
问题二:同样是A和B两个Activity,A跳转到B后,当B关闭时将B中的数据回传到A中:
效果图:
代码:
MainActivity类:
package com.test.activitytest;
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 MainActivity extends Activity {
private Button btn_get;
private TextView tv_city;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_get = (Button) findViewById(R.id.id_btn_getCity);
tv_city = (TextView) findViewById(R.id.id_tv_city);
btn_get.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,
SecondActivity.class);
// 启动指定Activity并等待返回的结果,其中0是请求码,用于标识该请求
startActivityForResult(intent, 0);
}
});
}
@Override
// 获取指定的Activity返回的结果
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
// 当requestCode,resultCode同时为0时,也就是处理待定的结果
if (requestCode == 0 && resultCode == 0) {
// 取出Intent里的Extras数据
Bundle bundle = intent.getExtras();
//取出Bundle中的数据
String result = bundle.getString("city");
tv_city.setText(result);
}
}
}
SecondActivity类:
package com.test.activitytest;
import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class SecondActivity extends ExpandableListActivity {
//定义省份数组
private String[] provinces = new String[]{
"山东","湖南","广东"
};
private String[][] cities = new String[][] {
{"东营","济南","青岛"},
{"衡阳","长沙","岳阳"},
{"广州","深圳","中山"}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ExpandableListAdapter adapter = new BaseExpandableListAdapter() {
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
//该方法决定每个组选项的外观
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(SecondActivity.this);
ll.setOrientation(0);
ImageView logo = new ImageView(SecondActivity.this);
ll.addView(logo);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
ll.addView(textView);
return ll;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
return provinces.length;
}
@Override
//获取指定组位置处的组数据
public Object getGroup(int groupPosition) {
return provinces[groupPosition];
}
@Override
public int getChildrenCount(int groupPosition) {
return cities[groupPosition].length;
}
@Override
//该方法决定每个子选项的外观
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
//获取指定组位置,指定子列表项处的子列表项数据
public Object getChild(int groupPosition, int childPosition) {
return cities[groupPosition][childPosition];
}
};
//设置该窗口显示列表
setListAdapter(adapter);
getExpandableListView().setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
//获取启动该Activity之前的Activity对应的Intent
Intent intent = getIntent();
intent.putExtra("city", cities[groupPosition][childPosition]);
//设置该Activity的结果码,并设置结束之后退回的Activity
SecondActivity.this.setResult(0,intent);
//结束该Activity
SecondActivity.this.finish();
return false;
}
});
}
private TextView getTextView(){
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);
TextView textView = new TextView(SecondActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL|Gravity.LEFT);
textView.setPadding(36, 0, 0, 0);
textView.setTextSize(20);
return textView;
}
}
上边的实例就是从第一个Activity跳转到第二个Activity,然后将第二个Activity中的数据回传给第一个Activity,并在第一个Activity中显示出回传过来的数据。
收获:
(1)有两个Activity(A和B),当A启用指定B,而且期望获取指定的B返回的结果的时候,A可以使用startActivityForResult(Intent intent,int requestCode)方法来启用指定的B。
(2)为了获取B返回的结果,需要使用下面的方法:
1. A需要重写onActivityResult(int requestCode,int resultCode,Intent intent),当B返回结果的时候,该方法就会被触发。requestCode—请求代码,resultCode—返回结果码。
2. B需要调用setResult()方法设置处理结果。
(3)上边设置requestCode和resultCode的原因:
一个Activity中可能包含多个按钮,并调用多个startActivityForResult()方法来打开多个不同的Activity处理不同的业务,这些新的Activity关闭后,系统都将会回调前面的Activity的onActivityResult(int requestCode,int ResultCode,Intent intent)方法。
为了知道该方法是由哪个请求的结果触发的,可利用requestCode请求码,为了知道返回的数据来自于哪个新的Acitivity,可利用ResultCode结果码。