Activity 初探 – 热爱改变生活
我的GitHub GitHub |     登录
  • If you can't fly, then run; if you can't run, then walk; if you can't walk, then crawl
  • but whatever you do, you have to keep moving forward。
  • “你骗得了我有什么用,这是你自己的人生”
  • 曾有伤心之地,入梦如听 此歌

Activity 初探

Android sinvader 5029℃ 0评论

Activity 的介绍免了(想看点这里),下面直接上代码:

Activity 的生命周期

在 MainActivity 以及 SecondActivity 中的各个生命周期方法中,用 log 的方式输出各自的方法名,根据输出的顺序,来观察生命周期方法调用的顺序:
MainActivity

  1. /**
  2. * @author sumile
  3. * @WEB https://sumile.cn
  4. * @2015 年 7 月 6 日 上午 10:58:06
  5. * @TODO
  6. */
  7. public class MainActivity extends Activity {
  8. private final String TAG = "sumile";
  9. private TextView tv;
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_main);
  15. Log.i(TAG, "MainActivity OnCreate");
  16. tv = (TextView) findViewById(R.id.tv);
  17. tv.setOnClickListener(new OnClickListener() {
  18.  
  19. @Override
  20. public void onClick(View v) {
  21. // 本方法于 2015 年 7 月 6 日 上午 11:04:48 由 sumile 建立
  22. Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  23. startActivity(intent);
  24. }
  25. });
  26. }
  27.  
  28. @Override
  29. protected void onStart() {
  30. // 本方法于 2015 年 7 月 6 日 上午 10:59:43 由 sumile 建立
  31. super.onStart();
  32. Log.i(TAG, "MainActivity onStart");
  33. }
  34.  
  35. @Override
  36. protected void onResume() {
  37. // 本方法于 2015 年 7 月 6 日 上午 10:59:49 由 sumile 建立
  38. super.onResume();
  39. Log.i(TAG, "MainActivity onResume");
  40.  
  41. }
  42.  
  43. @Override
  44. protected void onPause() {
  45. // 本方法于 2015 年 7 月 6 日 上午 10:59:55 由 sumile 建立
  46. super.onPause();
  47. Log.i(TAG, "MainActivity onPause");
  48.  
  49. }
  50.  
  51. @Override
  52. protected void onStop() {
  53. // 本方法于 2015 年 7 月 6 日 上午 11:00:00 由 sumile 建立
  54. super.onStop();
  55. Log.i(TAG, "MainActivity onStop");
  56.  
  57. }
  58.  
  59. @Override
  60. protected void onDestroy() {
  61. // 本方法于 2015 年 7 月 6 日 上午 11:00:03 由 sumile 建立
  62. super.onDestroy();
  63. Log.i(TAG, "MainActivity onDestroy");
  64.  
  65. }
  66.  
  67. @Override
  68. protected void onRestart() {
  69. // 本方法于 2015 年 7 月 6 日 上午 11:00:07 由 sumile 建立
  70. super.onRestart();
  71. Log.i(TAG, "MainActivity onRestart");
  72.  
  73. }
  74.  
  75. }

SecondActivity

  1. /**
  2. * @sumile
  3. * @WEB https://sumile.cn
  4. * @2015 年 7 月 6 日 上午 11:01:44
  5. * @TODO
  6. */
  7. public class SecondActivity extends Activity {
  8. private final String TAG = "sumile";
  9.  
  10. @Override
  11. protected void onCreate(Bundle savedInstanceState) {
  12. super.onCreate(savedInstanceState);
  13. setContentView(R.layout.activity_second);
  14. Log.i(TAG, "SecondActivity OnCreate");
  15. }
  16.  
  17. @Override
  18. protected void onStart() {
  19. // 本方法于 2015 年 7 月 6 日 上午 10:59:43 由 sumile 建立
  20. super.onStart();
  21. Log.i(TAG, "SecondActivity onStart");
  22. }
  23.  
  24. @Override
  25. protected void onResume() {
  26. // 本方法于 2015 年 7 月 6 日 上午 10:59:49 由 sumile 建立
  27. super.onResume();
  28. Log.i(TAG, "SecondActivity onResume");
  29.  
  30. }
  31.  
  32. @Override
  33. protected void onPause() {
  34. // 本方法于 2015 年 7 月 6 日 上午 10:59:55 由 sumile 建立
  35. super.onPause();
  36. Log.i(TAG, "SecondActivity onPause");
  37.  
  38. }
  39.  
  40. @Override
  41. protected void onStop() {
  42. // 本方法于 2015 年 7 月 6 日 上午 11:00:00 由 sumile 建立
  43. super.onStop();
  44. Log.i(TAG, "SecondActivity onStop");
  45.  
  46. }
  47.  
  48. @Override
  49. protected void onDestroy() {
  50. // 本方法于 2015 年 7 月 6 日 上午 11:00:03 由 sumile 建立
  51. super.onDestroy();
  52. Log.i(TAG, "SecondActivity onDestroy");
  53.  
  54. }
  55.  
  56. @Override
  57. protected void onRestart() {
  58. // 本方法于 2015 年 7 月 6 日 上午 11:00:07 由 sumile 建立
  59. super.onRestart();
  60. Log.i(TAG, "SecondActivity onRestart");
  61.  
  62. }
  63. }

运行成功后,在不同操作下,生命周期方法的调用顺序如下:

¥ 有帮助么?打赏一下~

转载请注明:热爱改变生活.cn » Activity 初探


本博客只要没有注明“转”,那么均为原创。 转载请注明链接:sumile.cn » Activity 初探

喜欢 (2)
发表我的评论
取消评论
表情

如需邮件形式接收回复,请注册登录

Hi,你需要填写昵称和邮箱~

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址