Android Studio Tutorial: [5] life cycle of an activity

Source: Internet
Author: User

To learn about Android, you have to understand the life cycle of an Android software, understand the process of creating, starting, stopping, pausing, restarting, and destroying an activity, knowing what functions are called at each stage to handle different situations, and here I have a simple example of how the life cycle of an activity can be known.

Tools/Materials
    • Android Studio
    • An Android phone or Android simulator
How Android Studio Overrides functions
  1. 1

    In Android Studio, if you need to re-function, you can select the first item "Override methods" under "Code" in the menu bar, or press Ctrl+o to open it.

  2. 2

    Find the function you want to rewrite in the pop-up list, take OnStart () as an example, and don't see a headache without a search port, just enter the desired function directly in the current list.

  3. 3

    You can then see the re-functions that have been generated, each with a @override flag in front of each overriding function. Add a sentence in the generated code:

    LOG.D (Tag, "in the OnStart () event");

    As a result, an active start rewrite function is completed, and other functions can be overridden in the same way.

  4. 4

    In the Demoactivity.java of the created project, add the following in the class:

    String tag = "Lifestyle";

    Add code to the auto-generated oncreate:

    LOG.D (Tag, "in the OnCreate () event");

  5. 5

    Rewrite the functions as described above separately:

    OnStart, Onresume, Onrestart, OnPause, OnStop, OnDestroy.

    In each overriding function, add the following separately:

    LOG.D (Tag, "in the OnStart () event");

    LOG.D (Tag, "in the Onresume () event");

    LOG.D (Tag, "in the Onrestart () event");

    LOG.D (Tag, "in the OnPause () event");

    LOG.D (Tag, "in the OnStop () event");

    LOG.D (Tag, "in the OnDestroy () event");

    END
Life cycle of activities
    1.  

      Connect an Android phone for simulation testing, shift+ F10 Build the APK and install it automatically on your phone.

    2.  

      When started, the activity needs to be created, started, continued three stages, corresponding to OnCreate, OnStart, Onresume of three functions. Onresume can continue, restart, and restore the threefold meaning.

      To view the status of the current activity in the Logcat window.

       

    3.  

      When another activity is displayed before the current activity, or the Auto lock screen, the current activity is paused, The Onresume function is called when the OnPause function is called to return to the current activity.

    4.  

      When you return to your phone's home page, the current activity will go from Onresume state through OnPause to OnStop. Indicates that the current activity is not visible and is stopped.

    5.  

      But when reopened, the activity is again returned by the stop state through Onrestart,onstart and back to the Onresume state.

    6.  

      If you press the back key to exit the program, the activity will be onresume by OnPause State, OnStop finally calls the OnDestroy destruction activity.

    7.  

      The first picture of this experience shows you how a life cycle of an activity is switched, and illustrates how activities are handled in different states by example. I believe you have a good understanding of the life cycle of activities. This way you can grasp the essentials of program writing and handle the right things in the right functions, which will make your app more perfect.

Android Studio Tutorial: [5] life cycle of an activity

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.