¶ÁÊéÈË

Android ActivityΪʲôҪϸ»¯³öonCre

·¢²¼Ê±¼ä£º 2013-10-21 17:00:48 ×÷Õߣº rapoo

Android ActivityΪʲôҪϸ»¯³öonCreate¡¢onStart¡¢onResume¡¢onPause¡¢onStop¡¢onDesdroyÕâô¶à·½·¨ÈÃÓ¦ÓÃÈ¥ÖØÔØ£¿

Ô­´´Á´½Ó£ºhttp://blog.csdn.net/zhao_3546/article/details/12843477£¬×ªÔØÇë×¢Ã÷£¬Ð»Ð»¡£


×î½üÔÚÑо¿ActivityµÄÆô¶¯Á÷³Ì£¬ÀÏÂÞµÄblogÔÚ¿´£¬Ò²ÕÒÁËÆäËü×ÊÁÏѧϰ£¬Ò²¸ú¹ýAndroid4.3µÄÔ´Â룬

ÔÚ¸ú´úÂëµÄ¹ý³ÌÖУ¬Í»È»Ïëµ½ÏÂÃæµÄÕâ¸öÎÊÌ⣺

Android ActivityΪʲôҪϸ»¯³öonCreate¡¢onStart¡¢onResume¡¢onPause¡¢onStop¡¢onDesdroyÕâô¶à·½·¨ÈÃÓ¦ÓÃÈ¥ÖØÔØ£¿

ÍøÉÏÌ«¶à¸ù¾ÝAndroid¿ª·¢¹æ·¶·­Òë×ªÔØµÄÄÚÈÝ£¬¶¼²»ÊÇÎÒÏëÒªµÄ´ð°¸£¬ÄǾÍ×Ô¼º·ÖÎöÏ¡£


ÈçÏÂÊÇÒ»¶ÎµäÐ͵ÄActivity¼äÇл»µÄÈÕÖ¾£¬´ÓAActivityÇл»µ½BActivity£º

10-17 20:54:42.247: I/com.example.servicetest.AActivity(5817): onCreate() 1166919192 taskID=6610-17 20:54:42.263: I/com.example.servicetest.AActivity(5817): onStart() 1166919192 taskID=6610-17 20:54:42.263: I/com.example.servicetest.AActivity(5817): onResume() 1166919192 taskID=6610-17 20:54:46.997: I/com.example.servicetest.AActivity(5817): onPause() 1166919192 taskID=6610-17 20:54:47.021: I/com.example.servicetest.BActivity(5817): onCreate() 1166971824 taskID=6610-17 20:54:47.028: I/com.example.servicetest.BActivity(5817): onStart() 1166971824 taskID=6610-17 20:54:47.028: I/com.example.servicetest.BActivity(5817): onResume() 1166971824 taskID=6610-17 20:54:47.099: I/com.example.servicetest.AActivity(5817): onStop() 1166919192 taskID=66

µ±´¥·¢´ÓAActivityÇл»µ½BActivityʱµÄÈÕÖ¾ÈçÏ£º

10-17 20:54:46.997: I/com.example.servicetest.AActivity(5817): onPause() 1166919192 taskID=66
10-17 20:54:47.021: I/com.example.servicetest.BActivity(5817): onCreate() 1166971824 taskID=66
10-17 20:54:47.028: I/com.example.servicetest.BActivity(5817): onStart() 1166971824 taskID=66
10-17 20:54:47.028: I/com.example.servicetest.BActivity(5817): onResume() 1166971824 taskID=66
10-17 20:54:47.099: I/com.example.servicetest.AActivity(5817): onStop() 1166919192 taskID=66


ÏÈAActivityµÄonPause()±»µ÷Óã¬È»ºóÊÇBActivityµÄ³õʼ»¯Á÷³Ì£¨onCreate() --> onStart() --> onResume()£©£¬ÔÙÈ»ºóÊÇAActivityµÄonStop()±»µ÷Óá£

ÓеãÒâ˼£¬ÎªÊ²Ã´²»ÊÇÏÈAActivityµÄonPause()¡¢onStop()±»µ÷Óã¬È»ºóÔÙBActivityµÄ³õʼ»¯Á÷³Ì£¨onCreate() --> onStart() --> onResume()£©£¿

»òÕßÓÖΪʲô²»ÊÇÏÈBActivityµÄ³õʼ»¯Á÷³Ì£¨onCreate() --> onStart() --> onResume()£©£¬ÔÙAActivityµÄonPause()¡¢onStop()±»µ÷Óã¿


ÈçÏÂÊÇActivityµÄ¼¸¸ö¹Ø¼ü·½·¨µÄ×¢ÊÍ£º

void android.app.Activity.onCreate(Bundle savedInstanceState)
Called when the activity is starting. This is where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.net.Uri, String[], String, String[], String) to retrieve cursors for data being displayed, etc.
You can call finish from within this function, in which case onDestroy() will be immediately called without any of the rest of the activity lifecycle (onStart, onResume, onPause, etc) executing.


void android.app.Activity.onStart()

Called after onCreate — or after onRestart when the activity had been stopped, but is now again being displayed to the user. It will be followed by onResume.

void android.app.Activity.onResume()

Called after onRestoreInstanceState, onRestart, or onPause, for your activity to start interacting with the user. This is a good place to begin animations, open exclusive-access devices (such as the camera), etc.

Keep in mind that onResume is not the best indicator that your activity is visible to the user; a system window such as the keyguard may be in front. Use onWindowFocusChanged to know for certain that your activity is visible to the user (for example, to resume a game).


void android.app.Activity.onPause()

Called as part of the activity lifecycle when an activity is going into the background, but has not (yet) been killed. The counterpart to onResume.

When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause returns, so be sure to not do anything lengthy here.

This callback is mostly used for saving any persistent state the activity is editing, to present a "edit in place" model to the user and making sure nothing is lost if there are not enough resources to start the new activity without first killing this one. This is also a good place to do things like stop animations and other things that consume a noticeable amount of CPU in order to make the switch to the next activity as fast as possible, or to close resources that are exclusive access such as the camera.

In situations where the system needs more memory it may kill paused processes to reclaim resources. Because of this, you should be sure that all of your state is saved by the time you return from this function. In general onSaveInstanceState is used to save per-instance state in the activity and this method is used to store global persistent data (in content providers, files, etc.)

After receiving this call you will usually receive a following call to onStop (after the next activity has been resumed and displayed), however in some cases there will be a direct call back to onResume without going through the stopped state.


void android.app.Activity.onStop()

Called when you are no longer visible to the user. You will next receive either onRestart, onDestroy, or nothing, depending on later user activity.

Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause method is called.


Èç¹ûËùÓеijõʼ»¯¶¼ÔÚonCreate()ÖÐʵÏÖ£¬»áÓÐʲôÎÊÌ⣿

Ê×ÏÈ£¬ActivityµÄonCreate()±»µ÷ÓÃʱ£¬Activity»¹²»¿É¼û£¬Èç¹ûÒª×öһЩ¶¯»­£¬¼ÈÈ»ÊÓͼ»¹²»´æÔÚ£¬ÔÚonCreateÖÐÀ´Æô¶¯¶¯»­£¬Ã÷ÏÔÓÐÎÊÌ⣻

Æä´Î£¬AActivity Çл»µ½ BActivity£¬ÔÙÇл»µ½ AActivity£¨ÎÒÃǼٶ¨ÊÇAActivityµÄͬһ¸öʵÀý£©£¬ÓÉÓÚʵÀýÒѾ­´æÔÚ£¬ËùÒÔonCreate²»»áÔÙ±»µ÷Óã¬ÄÇAActivity´Óºǫ́Çл»ÖÁǰ̨ʱ£¬ÓпÉÄÜÐèҪһЩ³õʼ»¯£¬ÄǾÍû·¨ÔÙ±»µ÷Óõ½ÁË£¬Ò²ÓÐÎÊÌ⣻


Èç¹ûËùÓеijõʼ»¯¶¼ÔÚonStart()ÖÐʵÏÖ£¬»áÓÐʲôÎÊÌ⣿

Ê×ÏÈ£¬onCreate()×¢ÊÍÖУ¬ÊÇÃ÷È·½¨Òé setContentView()¡¢findViewById() ÒªÔÚ onCreate() Öб»µ÷Ó㬵«ÎÒʵ²âÁËһϣ¬ÔÚonStart()Öе÷Óà setContentView()¡¢findViewById() ¹¦ÄÜÒ²ÊÇÕý³£µÄ£»

Æä´Î£¬onStart() ±»µ÷ÓÃʱ£¬Activity¿ÉÄÜÊǿɼûÁË£¬µ«»¹²»Êǿɽ»»¥µÄ£¬onResume()µÄ×¢ÊÍÖж¼Ã÷È·µØËµÁËÕâ²»ÊÇActivity¶ÔÓû§ÊǿɼûµÄ×îºÃµÄָʾÆ÷£¬onStart() ÔÚÕâ֮ǰ±»µ÷Óã¬ÄÇÓÐÒ»Ð©ÌØÊâµÄ³õʼ»¯Ïà¹ØµÄÂß¼­ÔÚÕâÀï±»µ÷ÓÃÒ²»áÓÐÎÊÌâ¡£


Èç¹û°ÑËùÓеÄÈ¥³õʼ»¯¶¼ÔÚonStop()ÖÐʵÏÖ£¬»áÓÐʲôÎÊÌ⣿

1¡¢ ÔÚ onResume() µÄ×¢ÊÍÖУ¬½¨ÒéÊÇÔÚonResume()Öдò¿ª¶ÀÕ¼É豸£¨±ÈÈçÏà»ú£©£¬ÓëonResume()¶ÔÓ¦µÄÊÇonPause()£¬ËùÒÔËùÓеÄÈ¥³õʼ»¯²Ù×÷·ÅÔÚonStop()ÖÐÖ´ÐУ¬¿ÉÄÜ»áÒý³öеÄÎÊÌ⣻

2¡¢onStop() µÄ×¢ÊÍÖÐÃ÷È·µØÐ´ÁË£¬ÔÚÄÚ´æ²»×ã¶øµ¼ÖÂϵͳÎÞ·¨±£Áô´Ë½ø³ÌµÄÇé¿öÏ£¬onStop() ¿ÉÄܶ¼²»»á±»Ö´ÐС£

ÎÒµÄÀÏAndroidÊÖ»úµÄÏà»úÓ¦ÓÃÈç¹ûδÕý³£¹Ø±Õ£¬Ïà»úÔÚ²»ÖØÆôϵͳµÄÇé¿öϾÍÎÞ·¨ÔÙÕý³£Æô¶¯£¬¹À¼Æ¾ÍºÍÕâ¸ö»úÖÆÓйأ»Ïà»ú½ø³ÌÊDZ»Ç¿ÖÆÉ±µôµÄ£¬¶øµ¼ÖÂÈ¥³õʼ»¯²Ù×÷δ±»Õý³£Ö´ÐС£


Activity¼äÌø×ªÊ±£¬ÎªÊ²Ã´ÊÇÏÈAActivityµÄonPause()±»µ÷Óã¬È»ºóÊÇBActivityµÄ³õʼ»¯Á÷³Ì£¨onCreate() --> onStart() --> onResume()£©£¬ÔÙÈ»ºóÊÇAActivityµÄonStop()±»µ÷Óã¿

1¡¢ÔÚ onResume() µÄ×¢ÊÍÖУ¬½¨ÒéÊÇÔÚonResume()Öдò¿ª¶ÀÕ¼É豸£¨±ÈÈçÏà»ú£©£¬ÓëonResume()¶ÔÓ¦µÄÊÇonPause()£¬¹Ø±ÕÏà»úµÄ²Ù×÷Ò²Ó¦¸ÃÔÚ´Ë·½·¨Öб»µ÷Óã»·ñÔò£¬¿¼ÂÇÒ»ÏÂÈçϳ¡¾°£º

Èç¹ûAActivity´ò¿ªÁËÏà»ú£¬ÎÒÃǵã»÷ij°´Å¥ÒªÌø×ªµ½BActivityÖУ¬BActivityÒ²Ïë´ò¿ªÏà»ú£»¼ÙÉèAActivityµÄonPause() ÔÚ BActivityÆô¶¯ºóÔÙ±»µ÷Óã¬

ÄÇBActivity¸ù±¾¾ÍÎÞ·¨ÔÙÕý³£Æô¶¯Ïà»ú¡£

2¡¢onPause() µÄ×¢ÊÍÖУ¬Ò²Ã÷È·µØËµÁË£¬ÔÚÕâ¸ö·½·¨ÖÐÖ´ÐÐÍ£Ö¹¶¯»­µÈ±È½ÏºÄCPUµÄ²Ù×÷£¬Èç¹û²»ÏÈÖ´ÐÐÕâЩ²Ù×÷£¬¾ÍÏÈÆô¶¯ÐÂÓ¦Óã¬È»ºóÔÙÀ´Ö´Ðд˲Ù×÷£¬È·ÊµÊDz»ºÏÂß¼­£»

´ÓAActivityÇл»µ½BActivityµÄÈÕÖ¾ÈçÏ£º

10-17 20:54:46.997: I/com.example.servicetest.AActivity(5817): onPause() 1166919192 taskID=66
10-17 20:54:47.021: I/com.example.servicetest.BActivity(5817): onCreate() 1166971824 taskID=66
10-17 20:54:47.028: I/com.example.servicetest.BActivity(5817): onStart() 1166971824 taskID=66
10-17 20:54:47.028: I/com.example.servicetest.BActivity(5817): onResume() 1166971824 taskID=66
10-17 20:54:47.099: I/com.example.servicetest.AActivity(5817): onStop() 1166919192 taskID=66

´ÓÂß¼­µÄÍêÕûÐÔºÍÓû§ÌåÑéµÄ½Ç¶ÈÀ´·ÖÎö£¬ÕâÑùʵÏÖȷʵÊDZȽϺÏÀíµÄ£¬µ±Óû§´¥·¢Ä³Ê¼þÇл»µ½ÐµÄActivity£¬Óû§¿Ï¶¨ÊÇÏ뾡¿ì½øÈëеÄÊÓͼ½øÐвÙ×÷£¬

ÉÏÃæÒѾ­ËµÁË£¬ÔÚonResume()Ò»°ã»á´ò¿ª¶ÀÕ¼É豸£¬¿ªÆô¶¯»­µÈ£¬

µ±ÐèÒª´ÓAActivityÇл»µ½BActivityʱ£¬ÏÈÖ´ÐÐAActivityÖеÄÓëonResume()Ïà¶ÔÓ¦µÄonPause()²Ù×÷£¬±ÈÈç¹Ø±Õ¶ÀÕ¼É豸£¬¹Ø±Õ¶¯»­£¬»òÆäËüºÄ·ÑcpuµÄ²Ù×÷£»

ÒÔ·ÀÖ¹BActivityÒ²ÐèҪʹÓÃÕâЩ×ÊÔ´£¬¹Ø±ÕºÄCPUµÄ²Ù×÷£¬Ò²ÓÐÀûÓÚBActivityÔËÐеÄÁ÷³©¡£

µ×²ãÖ´ÐÐAActivityµÄonPause()ʱ£¬ÓÐÒ»¶¨µÄʱ¼äÏÞÖÆµÄ£¬µ±ActivityManagerService֪ͨӦÓýø³ÌÔÝÍ£Ö¸¶¨µÄActivityʱ£¬Èç¹û¶ÔÓ¦µÄonPause()ÔÚ500msÄÚ»¹Ã»ÓÐÖ´ÐÐÍ꣬ActivityManagerService¾Í»áÇ¿ÖÆ¹Ø±ÕÕâ¸öActivity¡£ÈçϾÍÊǶÔÓ¦µÄonPause()Ö´Ðг¬Ê±³£Á¿¶¨Ò壺

    // How long we wait until giving up on the last activity to pause.  This    // is short because it directly impacts the responsiveness of starting the    // next activity.    static final int PAUSE_TIMEOUT = 500;  // ¶¨ÒåÔÚActivityStack.javaÖÐ

AActivityÖбȽÏÏûºÄ×ÊÔ´µÄ²¿·Ö¹Ø±Õºó£¬ÔÙÇл»µ½BActivityÖÐÖ´ÐÐBActivityµÄ³õʼ»¯£¬ÏÔʾBActivityÖеÄView¡£

µ±BActivityÒѾ­Ö´ÐÐÏÔʾ³öÀ´ÁË£¬Óû§¿ÉÒÔ½»»¥£¬ºǫ́ÔÙÈ¥Ö´ÐÐAActivityµÄonStop()²Ù×÷£¬¼´Ê¹ÕâÀïÃæÓÐЩ±È½ÏºÄʱµÄ²Ù×÷£¬Ò²Ã»ÓйØÏµ£¬ÕâÊÇÔÚºǫִ́ÐÐËùÒÔÒ²²»Ó°ÏìÓû§µÄÌåÑé¡£

¶ÁÊéÈËÍø >Android

ÈȵãÍÆ¼ö