Android Activity

Source: Internet
Author: User

An Activity profile

Activity is a subclass of context

Activity is one of the four components used to display controls and user interaction

Activity is a layout container

Activity if no response within 5s causes an abnormal ANR (application not response)

Two new one activity

1. Direct or indirect succession of activity

2. Androidmanifest.xml application node The Activity Name property must be configured with the remaining optional

Three intentions to activate new activity

An explicit intent

Another activity for activating this app

StartActivity (new Intent (context, otheractivity.  Class));

Implicit intent

More activity to activate other apps can match multiple apps

Principle: Exactly match the intent-filter of the opponent (Action category data)

// System Dial startactivity (new Intent (intent.action_dial, Uri.parse ("tel:18627777777")));

Transfer of four intent data

Basic data types

// Pass New Intent (context, otheractivity.  Class); Intent.putextra ("name", "small white"); Intent.putextra ("Age", "startactivity"); intent ; // Take value Intent Intent == Intent.getstringextra ("name"); int age = Intent.getintextra ("Age", 0);

Object data type

The class must be made to implement the Parcelable interface or the serializable interface.

The Parcelable interface is recommended because it consumes less resources serializable is a Java serialization interface that consumes resources on Android

// Pass New Intent (context, otheractivity.  Class), Intent.putextra (new User ("small white"),startactivity (intent) ; // Take value Intent Intent == Intent.getparcelableextra ("user");

Five open an activity close and return data

Six activity life cycle

Storage of temporary data after seven activity is reclaimed by the system

When the system is actively killing the application, it is necessary to store the temporary data

 Public classMainactivityextendsappcompatactivity {/**first method of recovery **/@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //data is only available if the system kills Savedinstancestate.        if(NULL!=savedinstancestate) {            intpage = savedinstancestate.getint ("page"); LOG.I ("HUANG", "onCreate page=" +page); }    }    /**Save **/@Overrideprotected voidonsaveinstancestate (Bundle outstate) {Super. Onsaveinstancestate (outstate); //when the system is actively killing the application, OnPause () callsOutstate.putint ("page", 100); }        /**second method of recovery **/@Overrideprotected voidonrestoreinstancestate (Bundle savedinstancestate) {Super. Onrestoreinstancestate (savedinstancestate); //only after being killed by the system will it be called after OnStart ().        intpage = savedinstancestate.getint ("page"); LOG.I ("HUANG", "onrestoreinstancestate page=" +page); }}

Life cycle and correlative method of eight-way screen switching

Vertical screen, Horizontal screen

Activity first kills and then creates

Vertical screen

Activity first kills and then creates

Configure the screen to toggle activity does not kill only interface changes

Monitor for screen switching

 Public classMainactivityextendsappcompatactivity {@Override Public voidonconfigurationchanged (Configuration newconfig) {Super. onconfigurationchanged (Newconfig); //You must configure the screen to toggle activity does not kill the switchover before it is called        Switch(newconfig.orientation) { CaseCONFIGURATION.ORIENTATION_LANDSCAPE:LOG.I ("HUANG", "Horizontal screen");  Break;  CaseCONFIGURATION.ORIENTATION_PORTRAIT:LOG.I ("HUANG", "vertical screen");  Break; }    }}

Processing of the screen orientation in my development

Nine Task stacks

Task stack: Used to record user's action behavior

Stack: Advanced back out

Stack (Stack): StartActivity ()

Out stack (stack): Finish () and user press back key

When launching an application system, a task stack is assigned to the application

Ten-start Mode

SingleInstance Single Instance

Such activity will be stored in a single task stack and such instances will only have one reference

If the same activity is already present in the main task stack, then the activity will be reused and the activity re-active will be re-used to invoke activity inside the Onnewintent () method

As an example,

Aactivity, Bactivity, bactivity where Bactivity's startup mode is SingleInstance

The second bactivity will re-use the first bactivity

At this point the situation of the stack aactivity bactivity

Let me give you an example.

Aactivity, Bactivity, bactivity, cactivity where Bactivity's startup mode is SingleInstance

The second bactivity will re-use the first bactivity

The case of the stack is bactivity aactivity cactivity It's weird, right? I'm surprised to know it's like this in three real-time tests.

Let me give you a final example

Aactivity, Bactivity, aactivity, bactivity, cactivity where Bactivity's startup mode is SingleInstance

The second bactivity will re-use the first bactivity

At this point the situation of the stack bactivity aactivity aactivity cactivity This is also very strange

Singletask Single Task stack

If the same activity already exists in the task stack, then all activity on the activity will be destroyed and the activity reuse activity will invoke the activity inside the Onnewintent () method

Singletop Exclusive Top

If the same activity is already present at the top of the stack, then the activity reuse activity that will be reused on the top of the stack will call activity inside the Onnewintent () method

New activity if not on top of the stack

Standard (default)

Start an activity an instance reference to the activity will be placed on the task stack each time the launch creates a new instance

11 Intent.setflags () method parameter value meaning

Intent.flag_activity_no_history

Jump to ACTIVITY via intent if this intent adds Flag_activity_no_history flag

Such activity will be stored in a single task stack and such instances will only have one reference

If the same activity already exists in the main task stack, then the old activity is destroyed and new activity is created.

As an example,

Aactivity, bactivity, bactivity where jump bactivity intent added Flag_activity_no_history flag

The first bactivity will be destroyed. The second bactivity is a new one.

At this point the situation of the stack aactivity bactivity

Let me give you an example.

Aactivity, Bactivity, bactivity, cactivity where jump bactivity intent added Flag_activity_no_history flag

The first bactivity will be destroyed. The second bactivity is a new one.

At this point the situation of the stack aactivity cactivity

Let me give you a final example

Aactivity, Bactivity, aactivity, bactivity, cactivity where jump bactivity intent added Flag_activity_no_ History logo

The first bactivity will be destroyed. The second bactivity is a new one.

At this point the situation of the stack aactivity aactivity cactivity

Intent.flag_activity_clear_top

Jump to ACTIVITY via intent if this intent adds Flag_activity_clear_top flag

If the same activity already exists in the task stack, it will destroy the old activity and all the activity on it and create a new activity.

Intent.flag_activity_single_top

Jump to ACTIVITY via intent if this intent adds Flag_activity_single_top flag

If the same activity is already present at the top of the stack, then the activity reuse activity that will be reused on the top of the stack will call activity inside the Onnewintent () method

New activity if not on top of the stack

Intent.flag_activity_new_task

Jump to ACTIVITY via intent if this intent adds Flag_activity_new_task flag

General: Start an activity an instance reference to the activity will be placed on the task stack each time the launch creates a new instance

Special case: If you attempt to start an activity from an outside abnormal path of activity such as starting an activity from a broadcastreceiver, intent must add the flag_activity_new_task tag Otherwise the error

12 Onnewintent () method invocation Order

If not on top of the stack

Onnewintent (), OnStart (), Onresume ()

If it is already on top of the stack

Onnewintent (), Onresume ()

13 note

In general, Intent.setflags () of the target activity is not recommended while the activity also sets the startup mode

Especially intent.flag_activity_no_history Intent.flag_activity_clear_top Intent.flag_activity_single_top and SingleInstance Singletask Singletop Mixed together

Cause the task stack to be weird. The method calls in the activity are also very strange.

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.

Tags Index: