Android four load mode. Safe

原文链接: http://unknownerror.net/2011-05/android-four-load-mode-safe-7929

Suggest first read the following two articles, such ability can better understand the safe load mode:

Android process, thread model
http://www.cnblogs.com/ghj1976/archive/2011/04/28/2031586.htmlOne single thread of "Android" description, understand model some of safe matters needing attention.

The relationship between time apply Android noted
http://www.cnblogs.com/ghj1976/archive/2011/04/29/2032412.htmlEspecially must understand what's time.

A Activty lifecycle

The life cycle Activty is also the process which the life cycle.

image

Each Activity (at a safe) state, as developers, is unable to control the application in a certain state, these are accomplished by the system.
But when an active state changes, developers can by calling onXX () method get related notification.

When in the realization of safe, covered by (override) these methods can be treated in you need to call.

  • OnCreate: when the activity first start, trigger this method, which can be completed in this activity initialization.
    OnCreate method has a parameter, this parameter can be null (null), also can be invoked onSaveInstanceState () method before preserved status information.
  • The onStart: this method will be subordinate activities trigger says show users.
  • OnResume: when a activities and user interaction, happened to trigger this method.
  • OnPause: when a are receptionist operation activities for other activities need to front desk running into the background, trigger the method.This time the state needs will activities, such as being edited for persistent database records.
  • OnStop: when a activity no longer need to show users, trigger the method.If memory nervous, the system will be directly to end this activity, but not trigger onStop method.So keeping state information is supposed to do when onPause, rather than when onStop.Activities if not in the foreground to run, will be to stop or Linux management process in order to give new activities reserve enough storage space and end at anytime these activities.Therefore as developers, in design application, must always remember this principle.In some cases, onPause method is perhaps the last method activities trigger, therefore the developers need at this time needed to hold information. Preservation
  • OnRestart: when a standstill activities need to show users, trigger the method.
  • OnDestroy: when activity destroyed, trigger the method.And as onStop method, if memory nervous, system will directly to end this activity and will not trigger this method.
  • OnSaveInstanceState: system call this method, allowing activities to save before state, for example in a string string's place the cursor.
    Normally, the developers don't need overwriting the method, the realization of the default, has provided automatically save activities involved in the user interface components all state information.

Safe stack

Above mentioned developers is unable to control the state of safe, that the state is safe to operate in accordance with what logic?This will know safe stack.

Each state is safe by it in safe stack (is a last-in-first-out LIFO, including all the safe running of the position queue decision.

When a new safe startup, current activities will move to safe safe at the top of the stack.

If the user use the back button return of words, or the end, on the stack safe will move up the safe and into mobile state.As below:

image

An application's priority is affected by the highest priority safe influence.When deciding whether an application to end to release resources, Android memory management use stack to decisions based on safe application priority.

Safe state
Generally considered safe has the following four state:

Activities: when a safe, it is in the stack and focus, visual can accept user input.Android trying to do everything possible to keep it safe state, kill other activities to ensure the safe current activities have the resources can be used.When another safe is enabled, this will be suspended.
Halt: in many cases, your safe visible but it didn't focus, in other words it is suspended.Possible reason is a transparent or the full screen's safe is activated.
When suspended, a safe will still be as active state, just can't accept user input.In very special circumstances, Android will kill a pause for activities of safe safe provide adequate resources.When a safe become completely hidden, it would become stop.
Stop: when a safe not visible, it "stop".The safe will still kept it in memory of all state and membership information.Nonetheless, when other places need memory, it will be most likely be release resources.When a safe, a very important stop the step is to save data and the current UI state.Once a safe exit or closed, it will be to use state.
To use: in a safe killed before and after, it is packed in the state for later use.To use safe Acitivity has been removed, and need in the display stack before restarting it available.

The four loading mode safe

More on android development process, safe the jump between may need to be safe: there are several ways, sometimes is ordinary generate a new example, sometimes want to jump to the original case, not a safe form a safe repetitive.Loading mode which way is decided to start a jump to originally a safe examples.

In android, there are four kinds of safe start-up mode, respectively is:

  • Standard: the standard model, a call startActivity () method can produce a new samples.
  • SingleTop: if there has been a example is located in the top of safe stack, will not produce new examples, and just call the safe newInstance () method.If not located in stack will generate a new samples.
  • In a new singleTask: will produce this example, time after each invocation use this, not to produce new examples.
  • SingleInstance: this is basically the same with singleTask, there is only one difference: in this mode of the safe place in examples, and there is only this time people safe, there can be other examples of examples.

These startup mode can AndroidManifest in features list document setting in the XML, j launchMode attribute the > < safe.

Relevant code has some sign can use, such as we want to just opening a example, Intent. Can be used FLAG_ACTIVITY_REORDER_TO_FRONT logo, the sign says: if this safe has launched, will not produce new safe, but onlyadd to the safe examples to stack.

Intent intent = new Intent(ReorderFour.this, ReorderTwo.class);  
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);  
startActivity(intent);  

The loading patterns are safe in the safe Intent objects starting Flag and then set the < file > element safe safe characteristics of value interactive control.

Below are some characteristics of the impact loading mode

The core Flag: Intent
FLAG_ACTIVITY_NEW_TASK
FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
FLAG_ACTIVITY_SINGLE_TOP
The core of < > characteristics are: safe
taskAffinity
launchMode
allowTaskReparenting
clearTaskOnLaunch
alwaysRetainTaskState
finishOnTaskLaunch

The difference between four loading mode

The time difference between belongs

Usually, "standard" and "the goal of singleTop" safe, and received Intent of time in the same time the sender within, is who is calling it, it and who at the same time.

Unless FLAG_ACTIVITY_NEW_TASK Intent including parameters.If provides FLAG_ACTIVITY_NEW_TASK parameters, will start to other time.
"SingleTask" and "singleInstance" always put to restart the safe as a time of the root element, they won't be start-up to one other time.

Whether to allow multiple instances

"Standard" and "singleTop" can instantiate many times, and can exist in different time;This instantiated a time can include a multiple instances of the safe;
"SingleTask" and "singleInstance" is limited to generate a examples, and is the root element time.
The intent singleTop requirements if created when stack to create safe have examples of the intent, it will send the instance, not to create new samples.

Whether to allow other exists within this time people safe

"SingleInstance" exclusive a safe, other time can not live that time;

If it launched a new safe, no matter how the new safe 2002.6 mode, the new safe to other time will run (as in FLAG_ACTIVITY_NEW_TASK parameters) added.
And the other three modes, can coexist with other safe.

Every new generation whether examples

"Standard" for each Intent will generate a safe start the new instance;
"SingleTop" safe if the stack in time, then don't generate a new word examples of the safe, direct use examples of stack, otherwise, generating the examples of safe.

For example:

Time now for A - B stack elements - c-d (D), this time in the stack to send A startup intent, the D is "standard" if D, then generate A new examples, the D stack into A - B - c-d - D.
If D singleTop words, is not is the new example, production D is still A stack state B - c-d
If this time B hair, no matter the B word of Intent launchmode is "standard" or "singleTop", will generate new examples of B, stack state into A - B - c-d - B.

"SingleInstance" is the only safe in the stack, it every time be re-used.

"SingleTask" if the intent is to accept stack, otherwise, the intent is discarded, but this time will still be returned to the reception.When existing safe, tackle new intent examples will call onNewIntent () method, if received intent generates a safe examples, so users can through the back key to return to a state;If it is a pre-existing safe to deal with the intent of words, the user can't by pressing the back button to return to this before the state.

Reference material

One of the Android safe the longer safe life cycle
http://winuxxan.blog.51cto.com/2779763/502523

Android's puzzle the second safe safe adding mode
http://winuxxan.blog.51cto.com/2779763/504047

Android safe life cycle
http://www.haoni.org/2011/02/25/androidactivityshengmingzhouqi/

Android in the four loading mode safe
http://blog.csdn.net/pcwings/archive/2010/09/19/5895197.aspx

Distinguish between the four loading mode safe
http://marshal.easymorse.com/archives/2950

Hello Android third edition (2)
http://blog.csdn.net/cqwty/archive/2010/09/08/5870219.aspx

Generate only a safe examples
http://www.eoeandroid.com/home-space-uid-43043-do-blog-id-6.html

The activation method (safe 2002.6 mode)
http://liubin.nanshapo.com/2010/12/23/activity-launch-mode/

Android app model: applications, task, process and thread
http://blog.csdn.net/iefreer/archive/2009/08/18/4460196.aspx


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值