Android Application Development Lecture 4: getting started with activity

Source: Internet
Author: User

Content of this section
Understanding Activity
Activity Lifecycle
Instance: Observe activity status changes caused by screen flip

Click here to download: lesson4.zip

1. Understand Activity

Activity is one of the four main components of the android program.
Activity is the presentation layer of the android program. Each display screen of the program is an activity.
Students who have learned web development can understand activity as a JSP file on a webpage, or you can understand activity as a Windows window.

Next, let's take a look at the inheritance relationship of the activity class:


We can see that activity is a subclass of the context class.

2. Understand the lifecycle of an activity

The most important and basic function of a mobile phone is to call the phone. This means that the current program may be suspended at any time when the phone comes. If the power is insufficient, the existing program may be closed at any time; therefore, the android program differs from the program on the computer. Specifically, the activity lifecycle is not controlled by itself, but controlled by the Android system.

In Android, activity has four basic states:
1. Running
At the front of the screen, it is visible and interactive with the user.
2. paused
When acitivy is overwritten by another transparent or non-full-screen activity, the status is paused. Although visible, it cannot interact with each other.
3. Stop
When the activity is overwritten by another activity and the interface is invisible, it is in the Stop State.
4. Killed
The activity is killed by the system or is in the killed state when it is not started.

Let's take a look at the lifecycle of an activity:


The following is a simple translation version:

Activity Stack:
Android uses the activity stack to manage the activity.

The running activity is at the top of the stack and is running;

When a new activity enters the top of the screen, the original activity is pushed to the second layer. If the screen is not completely covered, the activity is in the pause status, if it is hidden, it is in the Stop State.

Of course, you may be forced to shut down when the system feels that resources are insufficient, and of course, the program at the bottom of the stack will be shut down first.

For example, when you call the activity. Finish () method in a program, the result is the same as if you press the back key: the user tells activity manager that the activity instance can be recycled ". Then the activity Manager activates the activity at the second layer of the stack and re-enters the stack. It pushes the original activity to the second layer of the stack and switches from running to paused.

Iii. Example: Observe the activity status changes caused by screen flip

The activity class defines some methods related to the lifecycle. These methods are called when the status changes, for example, the oncreate () method called during creation (). Therefore, we can write a program, write comments in each method of the program, and then look at the print order of comments at run time to track activity status changes.

The following is the program code:

  1. Package Android. Basic. lesson4; import Android. App. activity;
  2. Import Android. OS. Bundle;
  3. Import Android. util. log; public class mainactivitylife extends activity {
  4. String tag = "[mainactivitylife]";
  5. /** Called when the activity is first created .*/
  6. @ Override
  7. Public void oncreate (bundle savedinstancestate ){
  8. Super. oncreate (savedinstancestate );
  9. Setcontentview (R. layout. Main );
  10. Log. I (TAG, "oncreate ");
  11. } @ Override
  12. Public void onstart (){
  13. Super. onstart ();
  14. Log. I (TAG, "onstart ");
  15. } @ Override
  16. Public void onpause (){
  17. Super. onpause ();
  18. Log. I (TAG, "onpause ");
  19. } @ Override
  20. Public void onresume (){
  21. Super. onresume ();
  22. Log. I (TAG, "onresume ");
  23. } @ Override
  24. Public void onstop (){
  25. Super. onstop ();
  26. Log. I (TAG, "onstop ");
  27. } @ Override
  28. Public void ondestroy (){
  29. Super. ondestroy ();
  30. Log. I (TAG, "ondestroy ");
  31. }
  32. }

Copy code

Then call the logcat tool to observe the running details of the program. Through the logcat filter, we can see the process of program startup, screen flip, and exit by pressing the back key, which helps us to verify what we just learned.



We can see that when the screen is flipped, the android system first kills the activity activitylife (the specific order is to pause first, then close and then destroy), and then starts (the specific order is to create first, and then resume ). Through this example, we can clearly see that the Android system is not the programmer controlling the lifecycle of the activity.

This lesson is here.

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.