"Turn" two minutes to fully understand the Android activity life cycle (graphics)!----good

Source: Internet
Author: User

Original URL: http://blog.csdn.net/android_tutor/article/details/5772285

Hello everyone, today to give you a detailed description of the activity of Android life cycle, I have said in front of this aspect of the content, but like most articles on the internet, basically are translated Android API, too general, I believe you see, there will be a little help, But it's not completely thoroughly understand, so I'm going to do it again today.

First look at the activity life cycle diagram provided in the Android API (not clear, you can see the full article, looking back at this diagram, you will understand):

The activity is actually inherited applicationcontext This class, we can rewrite the following method, the following code:

[Java]View Plaincopy
  1. Public class Activity extends ApplicationContext {
  2. protected void OnCreate (Bundle savedinstancestate);
  3. protected void OnStart ();
  4. protected void Onrestart ();
  5. protected void Onresume ();
  6. protected void OnPause ();
  7. protected void OnStop ();
  8. protected void OnDestroy ();
  9. }

In order to facilitate the better understanding of everyone, I simply wrote a demo, do not understand the activity cycle of friends, you can practice, everyone follow my steps.

First step: Create a new Android project, which I named Activitydemo.

Step Two: Modify Activitydemo.java (I have re-written the above seven methods, mainly with log printing), the code is as follows:

[Java]View Plaincopy
  1. Package Com.tutor.activitydemo;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import Android.util.Log;
  5. Public class Activitydemo extends Activity {
  6. private static final String TAG = "Activitydemo";
  7. public void OnCreate (Bundle savedinstancestate) {
  8. super.oncreate (savedinstancestate);
  9. Setcontentview (R.layout.main);
  10. LOG.E (TAG, "Start oncreate~~~");
  11. }
  12. @Override
  13. protected void OnStart () {
  14. Super.onstart ();
  15. LOG.E (TAG, "Start onstart~~~");
  16. }
  17. @Override
  18. protected void Onrestart () {
  19. Super.onrestart ();
  20. LOG.E (TAG, "Start onrestart~~~");
  21. }
  22. @Override
  23. protected void Onresume () {
  24. Super.onresume ();
  25. LOG.E (TAG, "Start onresume~~~");
  26. }
  27. @Override
  28. protected void OnPause () {
  29. Super.onpause ();
  30. LOG.E (TAG, "Start onpause~~~");
  31. }
  32. @Override
  33. protected void OnStop () {
  34. Super.onstop ();
  35. LOG.E (TAG, "Start onstop~~~");
  36. }
  37. @Override
  38. protected void OnDestroy () {
  39. Super.ondestroy ();
  40. LOG.E (TAG, "Start ondestroy~~~");
  41. }
  42. }

Step three: Run the above project as follows (nothing special):

Core in Logcat window, if you do not use logcat you can take a look at my article Log text (LOG.V,LOG.D,LOG.I,LOG.W,LOG.E), we opened the application has been executed OnCreate () OnStart ()->onresume three methods, take a look at the Logcat window as follows:

Back key:

When we press the back key, our application will end, and this time we will call OnPause ()->onstop ()->ondestory () Three methods as shown:

Home key:

When we opened the app, like a browser, I was browsing the NBA news, and when I saw half of it, I suddenly wanted to listen to the song, and then we chose to press the home button to open the Music app, and when we pressed home, the activity executed OnPause (). OnStop () These two methods, the application is not destroyed at this time. As shown in the following:

When we start the Activitydemo application again, the Onrestart ()->onstart ()->onresume () Three methods are executed separately, as shown in:

Here we will draw a question, when we press the Home button, and then enter the Activitydemo application, our application status should be the same as the state before the home button, also for the convenience of understanding, here I will activitydemo code to make some changes, is to add a edittext.

Fourth step: Modify the Main.xml layout file (add a edittext), the code is as follows:

[Java]View Plaincopy
  1. <?xml version="1.0" encoding="Utf-8"?>
  2. <linearlayout xmlns:android="Http://schemas.android.com/apk/res/android"
  3. android:orientation="Vertical"
  4. Android:layout_width="Fill_parent"
  5. android:layout_height="Fill_parent"
  6. >
  7. <textview
  8. Android:layout_width="Fill_parent"
  9. android:layout_height="Wrap_content"
  10. android:text="@string/hello"
  11. />
  12. <edittext
  13. android:id="@+id/edittext"
  14. Android:layout_width="Fill_parent"
  15. android:layout_height="Wrap_content"
  16. />
  17. </LinearLayout>

Fifth step: Then the other unchanged, run the Activitydemo program, enter in the EditText such as "Frankie" string (such as:)

At this time, you can press the Home button, and then start the Activitydemo application again, this time there is no edittext we entered the word "Frankie", such as:

This obviously cannot be called a qualified application, so we need to implement it ourselves in several methods of activity, as shown in the sixth step below:

The sixth step is to modify the Activitydemo.java code as follows:

[Java]View Plaincopy
  1. Package Com.tutor.activitydemo;
  2. Import android.app.Activity;
  3. Import Android.os.Bundle;
  4. Import Android.util.Log;
  5. Import Android.widget.EditText;
  6. Public class Activitydemo extends Activity {
  7. private static final String TAG = "Activitydemo";
  8. private EditText Medittext;
  9. //define a String type to access the value of our edittext input
  10. private String mstring;
  11. public void OnCreate (Bundle savedinstancestate) {
  12. super.oncreate (savedinstancestate);
  13. Setcontentview (R.layout.main);
  14. Medittext = (EditText) Findviewbyid (R.id.edittext);
  15. LOG.E (TAG, "Start oncreate~~~");
  16. }
  17. @Override
  18. protected void OnStart () {
  19. Super.onstart ();
  20. LOG.E (TAG, "Start onstart~~~");
  21. }
  22. //When you press the home key and then start the app again, we want to restore the previous state
  23. @Override
  24. protected void Onrestart () {
  25. Super.onrestart ();
  26. Medittext.settext (mstring);
  27. LOG.E (TAG, "Start onrestart~~~");
  28. }
  29. @Override
  30. protected void Onresume () {
  31. Super.onresume ();
  32. LOG.E (TAG, "Start onresume~~~");
  33. }
  34. //When we press the home key, I assign the input value to mstring in the OnPause method
  35. @Override
  36. protected void OnPause () {
  37. Super.onpause ();
  38. mstring = Medittext.gettext (). toString ();
  39. LOG.E (TAG, "Start onpause~~~");
  40. }
  41. @Override
  42. protected void OnStop () {
  43. Super.onstop ();
  44. LOG.E (TAG, "Start onstop~~~");
  45. }
  46. @Override
  47. protected void OnDestroy () {
  48. Super.ondestroy ();
  49. LOG.E (TAG, "Start ondestroy~~~");
  50. }
  51. }

Seventh step: Rerun the Activitydemo program, repeat the fifth step, when we press the home key, when the application is launched again, EditText has the last input "Frankie" word, as shown:

OK, the basic Gaocheng, this time everyone can look at the activity life cycle diagram, I think we should fully understand the activity life cycle, do not know you know?

"Turn" two minutes to fully understand the Android activity life cycle (graphics)!----good

Related Article

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.