Android Activity life Cycle

Source: Internet
Author: User

when users need to interact with the phone through the screen, such as making a phone call, taking a photo, sending an email, or viewing a map. Developers need to implement an activity. Each activity is used as a window to provide the user with an interface. It can fill the entire screen, or it can be smaller, larger than the screen, or even floating on top of other windows. As one of the four most important components in Android apps. Today we will learn about the life cycle of activity in Android .

First, create an Android project that contains two Activitya and Activityb
10 kinds of situation discussion

First, the normal start activity A, the output results are as follows:

Second, jump from Activitya to Activityb, and Activityb fullscreen. That is, the Activitya is completely covered by the ACTIVITYB and the output is as follows:

Thirdly, in the second case, returning from Activityb to Activitya, the output is as follows:

Four, jump from Activitya to Activityb, and activityb not full screen. That is, the Activitya is not completely covered by the ACTIVITYB, the output is as follows:

Five, in the case of the fourth kind, return from Activityb to Activitya, the output is as follows:

Six, in the first case, click the Home button, the output is as follows:

VII, in the sixth case, long press the home keyboard, from the task Manager to bring up the Activitya, the output is as follows:

In the first case, click the Power key and the output is as follows:

Ninth, in the case of the eighth type, when the Power key is turned back to Activitya, the output is as follows:

Tenth, exit Activitya, the output is as follows:

after discussing the above 10 kinds of situations, it may feel a little complicated, in fact, it is not complicated, in carding. First, the easiest thing to find is that OnCreate and OnDestroy were executed only once from beginning to end. OnCreate executes once when an activity first starts, and OnDestroy executes once when the activity is closed. It can also be found that except in the case of 452, the results of the remaining cases are the same, leaving activity a, then a executes onpause and OnStop, and when a is returned, the Onrestart, OnStart, and Onresume are executed one time. so we can draw the following conclusions. As long as it does not enter into other activities that are not full-screen, a executes onpause and onstop, whereas only one onpause is executed, returns from other activities, a executes Onrestart, OnStart, and onresume, whereas Onresume is executed.

When testing 452 cases, simply add the following line of code to the Androidmanifest.xml Squadron ACTIVITYB node:

Android:theme= "@android: Style/theme.dialog"

Attached Activity Activitya Code:

[Java]View Plaincopy
  1. Package Com.yuxianglong.view;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import Android.os.Bundle;
  5. import Android.util.Log;
  6. import Android.view.View;
  7. import Android.widget.Button;
  8. /**
  9.  * 
  10. * @ClassName: Activitya
  11. * @Description: Testing the activity life cycle, overloading the Oncreate,onstart,onresume,
  12. * Onstop,onpause
  13. * Seven Methods of Ondestroy,onrestart
  14. * @author Gyz
  15. * @date 2014-7-9 a.m. 10:55:37
  16.  * 
  17.  */
  18. Public class Activitya extends Activity {
  19. Private Button button;
  20. Private String Tag = "yuxlong2010";
  21. @Override
  22. protected void onCreate (Bundle savedinstancestate) {
  23. Super. OnCreate (savedinstancestate);
  24. LOG.D (Tag, "I ' m onCreate");
  25. Setcontentview (r.layout.activity_a);
  26. Button = (button) Findviewbyid (R.id.button);
  27. Button.setonclicklistener (new View.onclicklistener () {
  28. @Override
  29. Public void OnClick (View v) {
  30. //TODO auto-generated method stub
  31. StartActivity (new Intent (Activitya. this, activityb. class));
  32. }
  33. });
  34. }
  35. @Override
  36. protected void OnStart () {
  37. //TODO auto-generated method stub
  38. Super. OnStart ();
  39. LOG.D (Tag, "I ' m OnStart");
  40. }
  41. @Override
  42. protected void Onresume () {
  43. //TODO auto-generated method stub
  44. Super. Onresume ();
  45. LOG.D (Tag, "I ' m Onresume");
  46. }
  47. @Override
  48. protected void onStop () {
  49. //TODO auto-generated method stub
  50. Super. OnStop ();
  51. LOG.D (Tag, "I ' m onStop");
  52. }
  53. @Override
  54. protected void OnDestroy () {
  55. //TODO auto-generated method stub
  56. Super. OnDestroy ();
  57. LOG.D (Tag, "I ' m OnDestroy");
  58. }
  59. @Override
  60. protected void Onrestart () {
  61. //TODO auto-generated method stub
  62. Super. Onrestart ();
  63. LOG.D (Tag, "I ' m Onrestart");
  64. }
  65. @Override
  66. protected void OnPause () {
  67. //TODO auto-generated method stub
  68. Super. OnPause ();
  69. LOG.D (Tag, "I ' m onPause");
  70. }
  71. }

Android Activity life Cycle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.