Use of android-activity (ii)

Source: Internet
Author: User

Activity as one of the four components in Android is very important. There is a lot of activity in the app, what data structures are used to manage the activity? What is the difference between several startup modes of activity? There is also the life cycle of the activity, which will be explained in one by one.

1. Example

First activity

1  Public classMyActivityextendsappcompatactivity {2     PrivateButton Mbutton =NULL;3 @Override4     protected voidonCreate (@Nullable Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_my);7Mbutton =(Button) Findviewbyid (r.id.id_back);8Mbutton.setonclicklistener (NewView.onclicklistener () {9 @OverrideTen              Public voidOnClick (View v) { OneIntent Intent =NewIntent (myactivity. This, Mainactivity.class); A startactivity (intent); -             } -         }); the     } -}

A second activity

1  Public classMyActivityextendsappcompatactivity {2     PrivateButton Mbutton =NULL;3 @Override4     protected voidonCreate (@Nullable Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.activity_my);7Mbutton =(Button) Findviewbyid (r.id.id_back);8Mbutton.setonclicklistener (NewView.onclicklistener () {9 @OverrideTen              Public voidOnClick (View v) { OneIntent Intent =NewIntent (myactivity. This, Mainactivity.class); A startactivity (intent); -             } -         }); the     } -}

Effect

I click the Jump button, jump to the second activity, then click the Back button in the second activity, and jump to the first activity interface, so repeatedly. Finally, click the Back button on the phone button, the effect is as follows

When we click on the return button on the phone, will return it will return a lot of activity, in order to normally exit to the desktop, it is very confusing, we have only two activity, click on the phone's return button on the first interface and the second interface repeatedly jump? This is about the data structure of our activity management.

2.activity management data Structure--stack

(1) What is a stack

Stack is a type of data structure, in line with FIFO rules, only one import and export, but also into the stack (add data to the stack) and out of the stack (the first stack to fetch data) is an import and export access.

Into the stack:

Out Stack:

Stack of:

(2) Activity's Task stack

In an app for Android, all acitivty are maintained in a single task stack. When we use intent to jump from the first activit to the second activity, the first activity is fed into the stack and is the top of the stack. If the second activity uses intent to jump back, the first activity will not come out of the task stack, since the second activity uses intent to jump back, it will itself into the stack, it becomes the top of the stack itself. So, over and over again, there's a lot of the first activity and the second activity object in the stack,

When we start clicking on the return button on the phone, the system gets an activity object from the top of the stack in the task stack, gets the activity object, shows the activity's interface, and the object disappears in the stack, which explains why our previous example appeared.

(3) Emptying the task stack

In the Android development process, we sometimes need to empty all the activity objects in the task stack. For example, when we write off in an app, we definitely empty all the activity objects in the task stack, otherwise there might be some problems.

A. Using the flags of intent

When using intent, set the intent object to: Intent.addflags (Intent.flag_activity_new_task | Intent.flag_activity_clear_task); Through this sentence, we can clear all the ACTIVITY objects in the task stack.

B.cleartaskonlaunch

Cleartaskonlaunch, as the name implies, clears all activity objects above the activity every time the activity is returned. This property allows the task stack to have only one activity object for each initialization.

C.finshontasklaunch

The Finishontasklaunch attribute is similar to the Cleartaskonlaunch attribute, except that cleartaskonlaunch acts on others, and Finishontasklaunch on himself. With this property, when the user returns, the object is destroyed when it leaves the task where the activity is located.

d.alwaysretaintaskstate

The Alwaysretaintaskstate property gives the task a gold medal, and if the activity's property is set to True, the task in which the activity is located no longer accepts any cleanup commands.

Attention:

1. The activity in the same activity stack does not necessarily come from the same app, and all the activity of the same app is not necessarily in the same activity stack, which is said for brevity.

2. When we use startactivity and the like to jump an activity, if we call the finish () method, then the activity object will not go into the task stack

1                 New Intent (myactivity.  this, mainactivity. class ); 2                 startactivity (intent); 3                 MyActivity.this.finish ();

3.Activity Start-up mode

In Android, activity is represented by an interface, and each activity has its own startup mode. When you start learning Android, you may not know what the startup mode is, or how the startup mode works. Before we said that the activity has the corresponding task stack, in fact, the startup mode is about how the activity object is created and used in the task stack.

In Android, the boot mode is divided into 4 types, namely standard,singletop,singletask,singleinstance. We can configure each activity's startup mode in the Androidmanifest.xml file, which we registered in the Androidmanifest.xml file before we created an activity. When setting the activity's startup mode, set the startup mode in the <activity> tab through the Android:launchmode property.

1 <?XML version= "1.0" encoding= "Utf-8"?>2 <Manifestxmlns:android= "Http://schemas.android.com/apk/res/android"3  Package= "Com.example.android_demo">4 5     <uses-permissionAndroid:name= "Android.permission.FLASHLIGHT" />6     <uses-permissionAndroid:name= "Android.permission.VIBRATE"></uses-permission>7 8     <Application9         Android:allowbackup= "true"Ten Android:icon= "@mipmap/ic_launcher" One Android:label= "@string/app_name" A Android:roundicon= "@mipmap/ic_launcher_round" - Android:supportsrtl= "true" - Android:theme= "@style/apptheme"> the         <ActivityAndroid:name=". Mainactivity " -             android:launchmode= "Standard" -             > -             <Intent-filter> +                 <ActionAndroid:name= "Android.intent.action.MAIN"></Action> -                 <categoryAndroid:name= "Android.intent.category.LAUNCHER"></category> +             </Intent-filter> A  at         </Activity> -         <ActivityAndroid:name=". MyActivity "></Activity> -     </Application> - </Manifest>

(1). Standard

Standard mode is the default startup mode of activity, if we register an activity and do not set the startup mode, then the activity startup mode is the standard mode by default.

In standard mode, whenever a intent is used to initiate an activity, the object of the original activity is pressed into the stack, and the second activity object is created, regardless of the object that contains the activity in the stack. The example that we demonstrated earlier is this startup mode.

(2). Singletop

If you specify start activity as Singletop mode, then when starting a new activity, in the task stack, first determine whether the current top object is a new activity object, if so, do not create a new activity object, The object directly using the top of the stack is the line.

(3). Singletask

Singletask mode is similar to Singletop. Just singletop the object that determines the activity at the top of the current stack is the activity object that will be started, and if so, does not create the object that initiates the activity, and if not, creates While Singletask is the object that determines whether the entire task stack contains the activity to be started, if any, the object is not created, the object is moved to the top of the stack, and the object of activity above the object is destroyed; To create it. In summary, the Singletask startup mode guarantees that the activity object contains only one object in the task stack.

(4). singleinstance

SingleInstance This startup mode works much like the browser you use. When multiple programs access the browser, if the current browser is not open, then open the browser, otherwise it will be opened in the current browser. The activity of the startup mode SingleInstance appears in a new task stack, and the task stack contains only one activity object, which is the activity object.

Life cycle of 4.Activity

Here, only the people who touch Android must have questions about what is the life cycle. In fact, the life cycle of activity is like a person's life, people from birth to growth, then to aging, and finally to death. The life cycle of activity is similar to that of a person's life, and there is the process of birth (creation)-Growth (initiation)-Aging (tentative)-Death (destruction).

This is one of the official Google gives, which describes in detail the activity of the sound life cycle of each process.

The general process is: onCreate ()--OnStart ()--Onresume ()-->onpause ()--onStop ()--OnDestroy ();

(1). OnCreate ()

This method is called when the activity starts to create. We can do the initial exchange in this method, for example, binding layout, initializing events and so on.

(2). OnStart ()

When the activity invokes the OnCreate () method, the method is called, which is called when the acitivty is started.

(3). Onreume ()

This method is called when the activity is at the top level of the app, which is where the current user is interacting with the activity.

(4). OnPause ()

This method is ready for the activity to start or to restore other acitivty calls. We usually release some CPU resources in this method, and save some data, but this method must execute fast, otherwise it will affect the use of the new stack top activity. There is also the method that is called when a dialog is started.

(5). OnStop ()

This method is called when the activity is completely invisible, meaning that the activity is completely overwritten by something else.

(6). OnDestroy ()

This method starts the call when the activity is ready to be destroyed.

(7). Onrestart ()

This method is called during the activity from a completely invisible process.

Use of android-activity (ii)

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.