第一天上午重要知识点intent
一、在当前activity中利用intent启动其他activity
activity1中启动anctivity2,在onCreate方法中,上代码:
activity1:
Intent i = new Intent(activity1.this,activity2.class);
startActivity(i);
二、intent单向传数据,activity1到activity2
activity1:
Intent i = new Intent(activity1.this,activity2.class);
i.putExtra("txt","hello activity2");
startActivity(i);
activity2:
a = (TextView) findViewById(R.id.a);
a.setText(getIntent().getStringExtra("txt"));
三、 intent双向互传数据
activity1:
先在onCreate中
Intent i = new Intent(activity1.this,activity2.class);
Bundle data = new Bundle();
data.putString("txt","hello activity2");
i.putExtras(data);
startActivityForResult(i,0);
接下来创建函数onActivityResult();
protected void onActivityResult(int requestCode,int resultCode,Intent data){
String result = data.getStringExtra("result");
a.setText(result);
}
activity2:
Bundle data = getIntent().getExtras();
String txt = data.getString("txt");
Intent i = new Intent();
i.putExtras("result","hello activity1");
setResult(0,i);
书山有路勤为径!