Android Learning Note (10) life cycle of activity

Source: Internet
Author: User

How to define multiple activity in an application

-Define a class that inherits activity
-Carbon oncreate ()
Setcontentview (r.layout.secondlayout): Sets the layout file used by the activity
-Register the activity in the Androidmanifest.xml file
1) Add an activity tag in the application tag, name is usually "package name + class name", label is usually the activity name
2) If you want to modify the activity that the program starts by default, you need to move the intent-filter tag and its contents into the activity

Second, the way to start an activity

-Generate an Intent object (Intent)
    Intent Intent = new Intent ();

-Call the SetClass method to set the activity requested to start
    Intent.setclass (Mainactivity.this,secondactivity.class);
The first parameter of the SetClass function is a context object
Context is a class in which activity is a subclass of the context class, meaning that all activity objects can be transformed upward into a context object
The second parameter is a class object, which should pass in the class object of the activity that needs to be started in the current scene

-Invoke the StartActivity method to start activity
    StartActivity (Intent);

Third, the back stack in Android

The activated activity is placed in a stack, called the back stack.

Iv. life cycle function of activity

-oncreate, called the first time the activity is created
-onstart, called when activity becomes visible
-onresume, called when activity starts preparing to interact with the user
-onpause, called when the system is about to start another activity
-onstop, called when the current activity becomes invisible
-ondestroy, called before the current activity is destroyed
-onrestart, called when an activity is started again

V. Status of the Activity object

-resumed,activity object is in the running state
-paused, another activity is on the front, but this activity is still visible
-stopped, another activity on the front end that completely obscures the activity

Six, paired life cycle functions

Onpause<-->onresume
Onstop<-->onstart
Oncreate<-->ondestroy
Onrestart

Seven, take driving as an example

oncreate--> Buy a car
Ondestroy--> Car Scrap
Onstart--> Ignition
onstop--> stall
Onresume--> on the accelerator, driving the car forward
Onpause-->, loosen the throttle.

Android Learning Note (10) life cycle of 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.