WEB开发网
开发学院手机开发Android 开发 Android Activity跳转的小结 阅读

Android Activity跳转的小结

 2010-10-19 20:15:08 来源:WEB开发网   
核心提示:setResult(Activity.RESULT_OK,i);finish();第二个例子:demo2Main—>SecondActivity—>ThirdActivity—>Main这个例子的流程,比上一个更复杂一些,Android Activity跳转的小结(4),经历了3个Activity,最

setResult(Activity.RESULT_OK,i);

finish();

第二个例子:demo2

Main—>SecondActivity—>ThirdActivity—>Main

这个例子的流程,比上一个更复杂一些,经历了3个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_FIRST_USER){

if(data != null) {

request_text.setText(data.getStringExtra("request_text_for_third"));

}

}

}

}

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);

上一页  1 2 3 4 5 6 7 8 9  下一页

Tags:Android Activity 小结

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接