Intent, Bundle, ListView的使用
无参数的Activity跳转:
intent.setClass(MainActivity.this, InformationActivity.class);
startActivity(intent);
当前Activity A 向下一个Activity B跳转并传递数据:
Bundle bundle = new Bundle();
bundle.putString("name", "Jack");//参数是key和value
bundle.putString("age", "20");
Intent intent = new Intent();
intent.setClass(MainActivity.this, Informationactivity.class);
intent.putExtras(bundle);
startActivity(intent);
在Activity B中获取传递的数据:
Bundle bundle = this.getIntent().getExtras();
String name = bundle.getString("name");
String age = bundle.getString("age");
当前Activity A 向下一个Activity B跳转并要求回传数据:
//使用startActivityForResult函数来跳转
Intent intent = new Intent(Activity_A.this, Activity_B.class);
startActivityForResult(intent, REQUEST_CODE);
//在Activity A中重载onActivityResult函数,获取Activity B回传的数据
@Override
protected void onActivityResult(int requestCode,int resultCode, Intent idata){
if(requestCode == MY_REQUEST_CODE){
if(resultCode == Activity.RESULT.OK){
String name = idata.getExtras().getString("name");
String age = idata.getExtras().getString("age");
...
}
}
}
//在Activity B中调用setResult函数,回传数据
Bundle bundle = new Bundle();
bundle.putString("name", "Jack");
bundle.putString("age", "20");
Intent intent = new Intent(Activity_B.this, Activity_A.class);
intent.putExtras(bundle);
setResult(android.app.Activity.RESULT_OK, intent);
finish();