Android Activity跳转的小结
2010-10-19 20:15:08 来源:WEB开发网@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
request_text = (TextView)this.findViewById(R.id.request_text);
btn = (Button)this.findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Main.this,SecondActivity.class);
i.putExtra("request_text_for_main", "从Main传递到SecondActivity");
startActivityForResult(i, FIRST_REQUEST_CODE);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==FIRST_REQUEST_CODE && resultCode==Activity.RESULT_OK){
if(data != null) {
request_text.setText(data.getStringExtra("request"));
}
}
}
}
Java代码
public class SecondActivity extends Activity{
private TextView request_text;
private Button btn;
private String text;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
request_text = (TextView)this.findViewById(R.id.request_text);
if (getIntent().getStringExtra("request_text_for_main") != null) {
text = getIntent().getStringExtra("request_text_for_main");
request_text.setText(text);
}
btn = (Button)this.findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent();
i.putExtra("request",text+" "+"从SecondActivity传递到ThirdActivity");
setResult(Activity.RESULT_OK,i);
更多精彩
赞助商链接