多个Activity跳转的小结
在android开发中Activity相当于web开发的页面,或者说如j2ee开发中对应的jsp页面。在开发jsp时,经常会遇到页面跳转的问题,在android也同样会遇到。以下的几个例子是我对android的Activity跳转的小结,也是最近做项目的小结。
第一个例子:demo1
Main—>SecondActivity—>Main
从流程上看就是从Main跳转到SecondActivity,再从SecondActivity返回到Main。也就是将子Activity的数据返回给父Activity。
代码如下:
1. public class Main extends
2.
3. private
4. private
5. private final int FIRST_REQUEST_CODE = 1;
6.
7. /** Called when the activity is first created. */
8. @Override
9. public void
10. super.onCreate(savedInstanceState);
11. setContentView(R.layout.main);
12.
13. this.findViewById(R.id.request_text);
14.
15. this.findViewById(R.id.btn);
16. new
17. public void
18. new Intent(Main.this,SecondActivity.class);
19. "request_text_for_main", "从Main传递到SecondActivity");
20. startActivityForResult(i, FIRST_REQUEST_CODE);
21. }
22. });
23. }
24.
25. protected void onActivityResult(int requestCode, int
26. super.onActivityResult(requestCode, resultCode, data);
27. if(requestCode==FIRST_REQUEST_CODE && resultCode==Activity.RESULT_OK){
28. if(data != null) {
29. "request"));
30. }
31. }
32. }
33. }
1. public class SecondActivity extends
2.
3. private
4. private
5. private
6.
7. public void
8. super.onCreate(savedInstanceState);
9. setContentView(R.layout.second);
10.
11. this.findViewById(R.id.request_text);
12.
13. if (getIntent().getStringExtra("request_text_for_main") != null) {
14. "request_text_for_main");
15. request_text.setText(text);
16. }
17.
18. this.findViewById(R.id.btn);
19. new
20. public void
21. new
22. "request",text+"\n"+"从SecondActivity传递到ThirdActivity");
23. setResult(Activity.RESULT_OK,i);
24. finish();
25. }
26. });
27. }
28. }
图一
图一所示是demo初始化的样子,点击“第一次跳转”进入图二。
图二
图二显示从Main到SecondActivity这个过程,再点击“第二次跳转”即返回Main这个Activity。
图三
图三表示跳转结束,Button按钮上面的文字记录跳转的过程。
这个例子最主要是第一个Activity Main设置的onActivityResult()方法和startActivityForResult()方法。
在Main中通过startActivityForResult()调用SecondActivity,然后在onActivityResult()中获取其返回参数,或者说通过onActivityResult()进行回调。
SecondActivity中要返回第一个Main必须:
setResult(Activity.RESULT_OK,i);
finish();
第二个例子:demo2
Main—>SecondActivity—>ThirdActivity—>Main
这个例子的流程,比上一个更复杂一些,经历了3个Activity,最好跳转回第一个Activity。
代码如下:
1. public class Main extends
2.
3. private
4. private
5. private final int FIRST_REQUEST_CODE = 1;
6.
7. /** Called when the activity is first created. */
8. @Override
9. public void
10. super.onCreate(savedInstanceState);
11. setContentView(R.layout.main);
12.
13. this.findViewById(R.id.request_text);
14.
15. this.findViewById(R.id.btn);
16. new
17. public void
18. new Intent(Main.this,SecondActivity.class);
19. "request_text_for_main", "从Main传递到SecondActivity");
20. startActivityForResult(i, FIRST_REQUEST_CODE);
21. }
22. });
23. }
24.
25. protected void onActivityResult(int requestCode, int
26. super.onActivityResult(requestCode, resultCode, data);
27. if(requestCode==FIRST_REQUEST_CODE && resultCode==Activity.RESULT_FIRST_USER){
28. if(data != null) {
29. "request_text_for_third"));
30. }
31. }
32. }
33. }
1. public class SecondActivity extends
2.
3. private
4. private
5. private final int SECOND_REQUEST_CODE = 2;
6. private
7.
8. public void
9. super.onCreate(savedInstanceState);
10. setContentView(R.layout.second);
11.
12. this.findViewById(R.id.request_text);
13.
14. if (getIntent().getStringExtra("request_text_for_main") != null) {
15. "request_text_for_main");
16. request_text.setText(text);
17. }
18.
19. this.findViewById(R.id.btn);
20. new
21. public void
22. new Intent(SecondActivity.this,ThirdActivity.class);
23. "request_text_for_second", text+"\n"+"从SecondActivity传递到ThirdActivity");
24. startActivityForResult(i, SECOND_REQUEST_CODE);
25. }
26. });
27. }
28.
29. @Override
30. protected void onActivityResult(int requestCode, int
31. super.onActivityResult(requestCode, resultCode, data);
32. if(requestCode==SECOND_REQUEST_CODE && data != null){
33. setResult(Activity.RESULT_FIRST_USER, data);
34. finish();
35. }
36. }
37. }
1. public class ThirdActivity extends
2.
3. private
4. private
5. private
6.
7. public void
8. super.onCreate(savedInstanceState);
9. setContentView(R.layout.third);
10.
11. "request_text_for_second");
12. this.findViewById(R.id.request_text);
13. request_text.setText(text);
14.
15. this.findViewById(R.id.btn);
16. new
17. public void
18. new
19. "request_text_for_third", text+"\n"+"从ThirdActivity再次传递到Main");
20. setResult(Activity.RESULT_FIRST_USER, i);
21. finish();
22. }
23. });
24. }
25.
26. }
这个demo2比起前面的demo1的不同之处在于就是通过ThirdActivity进行跳转到Main。
在代码上的区别是:
(1)Main的onActivityResult()方法中,它的resultCode==Activity.RESULT_FIRST_USER而不是前面的resultCode==Activity.RESULT_OK
(2)SecondActivity中增加了onActivityResult()方法,该方法是用于回调ThirdActivity,并且该方法执行了跳转过程跳转到Main这个Activity。
(3) ThirdActivity和demo1的SecondActivity很类似。
第三个例子:demo3
Main—>SecondActivity—>ThirdActivity—>SecondActivity—>Main
这个过程看起来复杂,其实不然,最后一步是借助SecondActivity的另一个Button实现的。
代码如下:
1. public class Main extends
2. private
3. private
4. private final int FIRST_REQUEST_CODE = 1;
5.
6. /** Called when the activity is first created. */
7. @Override
8. public void
9. super.onCreate(savedInstanceState);
10. setContentView(R.layout.main);
11.
12. this.findViewById(R.id.request_text);
13.
14. this.findViewById(R.id.btn);
15. new
16. public void
17. new Intent(Main.this,SecondActivity.class);
18. "request_text_for_main", "从Main传递到SecondActivity");
19. startActivityForResult(i, FIRST_REQUEST_CODE);
20. }
21. });
22. }
23.
24. protected void onActivityResult(int requestCode, int
25. super.onActivityResult(requestCode, resultCode, data);
26. if(requestCode==FIRST_REQUEST_CODE && resultCode==Activity.RESULT_FIRST_USER){
27. if(data != null) {
28. "final_request"));
29. }
30. }
31. }
32. }
1. public class SecondActivity extends
2.
3. private
4. private
5. private final int SECOND_REQUEST_CODE = 2;
6. private
7.
8. public void
9. super.onCreate(savedInstanceState);
10. setContentView(R.layout.second);
11.
12. this.findViewById(R.id.request_text);
13.
14. if (getIntent().getStringExtra("request_text_for_main") != null) {
15. "request_text_for_main");
16. request_text.setText(text);
17. }
18.
19. this.findViewById(R.id.btn);
20. new
21. public void
22. new Intent(SecondActivity.this,ThirdActivity.class);
23. "request_text_for_second", text+"\n"+"从SecondActivity传递到ThirdActivity");
24. startActivityForResult(i, SECOND_REQUEST_CODE);
25. }
26. });
27.
28. this.findViewById(R.id.finalBtn);
29. new
30. public void
31. new
32. "final_request", text+"\n"+"从SecondActivity最后传递到Main");
33. setResult(Activity.RESULT_FIRST_USER,i);
34. finish();
35. }
36. });
37. }
38.
39. protected void onActivityResult(int requestCode, int
40. super.onActivityResult(requestCode, resultCode, data);
41. if(requestCode==SECOND_REQUEST_CODE && resultCode==Activity.RESULT_OK){
42. if(data != null) {
43. "request_text_for_third");
44. request_text.setText(text);
45. }
46. }
47. }
48. }
1. public class ThirdActivity extends
2.
3. private
4. private
5. private
6.
7. public void
8. super.onCreate(savedInstanceState);
9. setContentView(R.layout.third);
10.
11. "request_text_for_second");
12. this.findViewById(R.id.request_text);
13. request_text.setText(text);
14.
15. this.findViewById(R.id.btn);
16. new
17. public void
18. new
19. "request_text_for_third", text+"\n"+"从ThirdActivity再次传递到SecondActivity");
20. setResult(Activity.RESULT_OK, i);
21. finish();
22. }
23. });
24. }
25. }
比起前一个demo2其实没有更复杂之处,只是从ThirdActivity跳转回SecondActivity,而通过SecondActivity的另一个Button来跳转回Main的。