Android Activity跳转的小结
2010-10-19 20:15:08 来源:WEB开发网在android开发中Activity相当于web开发的页面,或者说如j2ee开发中对应的jsp页面。在开发jsp时,经常会遇到页面跳转的问题,在android也同样会遇到。以下的几个例子是我对android的Activity跳转的小结,也是最近做项目的小结。
第一个例子:demo1
Main—>SecondActivity—>Main
从流程上看就是从Main跳转到SecondActivity,再从SecondActivity返回到Main。也就是将子Activity的数据返回给父Activity。
代码如下:
Java代码
public class Main extends Activity {
private Button btn;
private TextView request_text;
private final int FIRST_REQUEST_CODE = 1;
/** Called when the activity is first created. */
@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"));
}
}
}
}
public class Main extends Activity {
private Button btn;
private TextView request_text;
private final int FIRST_REQUEST_CODE = 1;
/** Called when the activity is first created. */
更多精彩
赞助商链接