Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Android Activity launchMode     - Use the vi text editor and copy and paste Linux tips (Linux)

- floating IP in OpenStack neutron (Server)

- ORA-04031 Error Resolution (Database)

- Linux installed xdotool simulate keystrokes and mouse movements (Linux)

- To install the Contiki development toolchain on Ubuntu (Linux)

- Redis data types Introduction (Database)

- RabbitMQ user roles and access control (Linux)

- Linux, set and view environment variables (Linux)

- You can not ignore the seven Git tips (Linux)

- Java interview questions in nine radio (Programming)

- Android realize RippleEffect water (Programming)

- Docker Basic Concepts (Linux)

- TCP network communication Java Programming (Programming)

- How to use Git to upload code to GitHub project (Linux)

- How to release the cache memory on Linux (Linux)

- Fedora 20 users install the Mate 1.8 desktop (Linux)

- Timing Nginx logs cut and remove the log records of the specified number of days before (Server)

- Swift used in the application to add a local push at the specified time (Programming)

- ActiveMQ-based shared file system HA solutions (Server)

- Open source backup software installed on Ubuntu Systemback 1.6.201 (Linux)

 
         
  Android Activity launchMode
     
  Add Date : 2017-08-31      
         
         
         
  Android System Activity can say is like a design, it is good design in memory management, so that multi-task management capability in Android running on the system. But Activity by no means only start to show on the screen, it is also very demanding start-up mode, the paper speaks specifically describes many details of the Activity of the startup mode, to correct some erroneous view that development may help our in-depth understanding of Activity.

Before drafting

Before the formal wording, introduce some of the concepts mentioned in article

Follow-up articles will be mentioned Task, the Task here refers to a collection of user interaction Activity instance.
Task of Activity instances in the form of a stack of storage, this is the Activity stack fallback stack.
This picture is more, when the plug, please observe the title Activity at the top, to distinguish specific Activity.

Why there is a startup mode

Each application is different Activity of transaction processing. Mail client as an example, InboxActivity purpose is to show the Inbox, the Activity is not recommended to create multiple instances. The ComposeMailActivity is used to compose a message, you can instantiate multiple objects of this Activity. Activity rational design of objects whether to use an existing instance or repeatedly create, will make more good interaction design, but also to avoid a lot of problems. As the front of the goal you want to achieve, we need to use today's Activity startup mode.

how to use

Very simple to use, only need the corresponding element in the manifest Activity added android: launchMode attribute. The following code as

< Activity
    android: name = ". SingleTaskActivity"
    android: label = "singleTask launchMode"
    android: launchMode = "singleTask">
< / Activity>
Then there is the moment of introduction of the four values launchMode.

standard

This is the default value of launchMode, Activity does not contain android: launchMode or display is set to standard of Activity will be used in this mode.

Once set to this value, whenever there is a request Intent, will create a new instance of the Activity. For example, if there are 10 compose mail Intent, it will create 10 ComposeMailActivity instance to handle these Intent. The results are obvious, this model creates multiple instances of an Activity.

Android 5.0 before the performance

Activity of this newly generated instance of Intent sent into the top of the Task stack.

Show cross between calls, the new generation of Activity instances of Intent sent into the top of the Task stack, even though they belong to different programs.

But when we open the Task Manager, it will be a little strange, the task should show the Gallery, display interface is really another program Activity (Task because it is located at the top of the stack).

At this time, if we switch from Gallery application to the dial-up application, and then return to the Gallery, or to see the non Gallery of Activity, if we want to operate on the Gallery, you must press the Back button to return to the Gallery interface can. Indeed somewhat less reasonable.

Android 5.0 and after the performance

Activity for the same internal applications before starting and 5.0 show the same change is starting between different applications Activity justify the.

Start Activity between cross-application, creates a new Task, will be placed in the newly created Activity Task just created.

Meanwhile Task Manager to view the task seem more reasonable.

Suppose we existed before the test program, and then from the Gallery and share files to our test program, the corresponding task manager shows the following effects.

Usage scenarios: standard mode is suitable for this start to compose a message or social networking message Activity Post Activity. If you want every intent to create an Activity process, then this is to use the standard mode.

singleTop

singleTop fact, and almost the same standard, using singleTop the Activity can also create a lot of instances. The only difference is that if the target is already in the Activity called Task caller's stack, not create a new instance, instead of using the current instance of this Activity, and call this instance onNewIntent methods.

In singleTop this model, we need to handle onCreate and onNewIntent two methods applied this model Activity to ensure the normal logic.

scenes to be used

About singleTop a typical usage scenario is the search function. Suppose there is a search box, every time a search query will be directed to SearchActivity we see the results, in order to better interactive experience, we also place this search box at the top of the results page.

Hypothetically, SearchActivity startup mode for the standard, then each search will create a new SearchActivity instance, 10 times the query is 10 Activity. When we want to return to a non-SearchActivity, we need to press the return key 10 times, which is obviously too unreasonable.

But if we use singleTop, and if SearchActivity top of the stack, when a new query, not re-create SearchAc examples, but with the current SearchActivity to update the results. When we need to return to a non-SearchActivity simply press the return key once. Use the singleTop clearly than before to be reasonable.

to sum up

Only the caller and the target in the same Task Activity, and the target Activity top of the stack, using only existing target Activity instance, or the creation of a new target Activity instance.
If an external program starts singleTop of Activity, in Android 5.0 before the newly created Activity it will be located in the caller's Task, 5.0 and later will be placed in a new Task.
singleTask

singleTask this model and the previously mentioned standard and singleTop different. Use singleTask startup mode there will be only one instance of Activity in the system. If the instance already exists, intent is passed through to the Activity onNewIntent. Otherwise, the new Activity instance is created.

Within the same program

If the instance singleTask Activity system does not exist, you need to create an instance of this Activity, and this example will put the caller and the same Task and top of the stack.

If singleTask Activity instance already exists, then the Activity fallback stack, all located in the Activity Activity instances above are destroyed out (destruction process Activity lifecycle callbacks will be called), which makes singleTask Activity instances are in the stack. At the same time, Intent will be passed on to the SingleTask Activity instance by onNewIntent.

However, Google's documentation on singleTask such a description

The system creates a new task and instantiates the activity at the root of the new task.

Meaning creates a new Task for the system, and create an instance Activity into the bottom of the new Task.

But is not the case, in my case, singleTask Activity and created and placed where the caller Task, rather than placed in a new Task, use adb shell dumpsys activity can be verified.

1
2
3
4
5
6
7
8
9
Task id # 239
  TaskRecord {428efe30 # 239 A = com.thecheesefactory.lab.launchmode U = 0 sz = 2}
  Intent {act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] flg = 0x10000000 cmp = com.thecheesefactory.lab.launchmode / .StandardActivity}
    Hist # 1: ActivityRecord {429a88d0 u0 com.thecheesefactory.lab.launchmode / .SingleTaskActivity t239}
      Intent {cmp = com.thecheesefactory.lab.launchmode / .SingleTaskActivity}
      ProcessRecord {42243130 18965: com.thecheesefactory.lab.launchmode / u0a123}
    Hist # 0: ActivityRecord {425fec98 u0 com.thecheesefactory.lab.launchmode / .StandardActivity t239}
      Intent {act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] flg = 0x10000000 cmp = com.thecheesefactory.lab.launchmode / .StandardActivity}
      ProcessRecord {42243130 18965: com.thecheesefactory.lab.launchmode / u0a123}
However you want to describe the document is not impossible to achieve, we need to set launchMode singleTask the same time, plus taskAffinity attribute.

< Activity
    android: name = ". SingleTaskActivity"
    android: label = "singleTask launchMode"
    android: launchMode = "singleTask"
    android: taskAffinity = "">
< / Activity>
Completion of the above changes, we look at the changes in effect, Task of

At the same time, the system task manager will effect a corresponding change

Between the cross-application

When cross-application Intent to pass, if an instance singleTask Activity system does not exist, create a new Task speak, and then create an instance of SingleTask Activity will put a new Task.

System Task Manager will be changed as follows

If the application process singleTask Activity where the presence, but singleTask Activity instance does not exist, then start another application from the Activity, Activity new instance is created and put into Task belongs where in the process, and is located in the stack position.

More complex case, if singleTask Activity instance exists, from another program is started, then Task where the Activity will be moved to the top, and in this Task Located in all instances above singleTask Activity Activity will be normal destroys. If we press the return key, then we will fall back to the first Task Activity in the other, until the current Task of Activity fallback stack is empty, it will be returned to the caller of the Task.

The album starts when Task2 Task1 share call the singleTask Activity, and the Activity instance exists and is located Task1 fallback stack the third position (from top to bottom order), then located above two Activity Activity examples will be destroyed out so that the top of the stack Activity instance. At this point Task1 Fallback stack only two Activity, if you click to return, it will not retreat to the Photos app, but the stack position following singleTask Activity Activity, click again to return to return to the album before application.

scenes to be used

This mode uses Scene more similar to a mail client timelines Activity Inbox or social applications. Both scenarios require corresponding Activity only one instance can be maintained, but should be used with caution in this mode, because it can be destroyed in case the user is not aware off other Activity.

singleInstance

This mode and singleTask almost, because they are in the system only one instance. The only difference is stored Task singleInstance Activity instance can only store a pattern of the Activity instance. If you start another Activity from singleInstance Activity instance, then this will be placed in other instances Activity Task. Similarly, if singleInstance Activity is started by Activity, which will be different from the caller into the Task.

Although it is two task, but the system's task manager, has always displayed a, which is located at the top of the Task.

In addition, when we enter the application from the Task Manager can not be returned by the key goes back to Task1.

Fortunately, there are ways to solve this problem is taskAffinity mentioned before = "" for launchMode of Activity for the singleInstance join this attribute.

< Activity
    android: name = ". SingleInstanceActivity"
      android: label = "singleInstance launchMode"
    android: launchMode = "singleInstance"
    android: taskAffinity = "">
< / Activity>
Run again to modify the code to view the Task Manager, so the result is reasonable.

Usage

The use of this model is relatively rare, may be used in the Launcher. Or you need to make sure you have only one instance of Activity. We recommend caution.

Intent Flags

In addition to setting launchMode in the manifest file, you can also set the flag to achieve the same effect in the Intnet. As the following code to make StandardActivity has singleTop mode.

Intent intent = new Intent (StandardActivity.this, StandardActivity.class);
intent.addFlags (Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity (intent);
     
         
         
         
  More:      
 
- Debian 7.7 Installation and Configuration (Linux)
- Python MySQL database connection (Database)
- Linux disk and File System Concepts (Linux)
- Oracle 11g contraction table space error ORA-03297: file contains used data beyondrequested RESIZE value (Database)
- Linux firewall iptables beginner tutorial (Linux)
- Reported too many open files Linux solutions (Server)
- Those functions under Linux you do not know the df command (Linux)
- Workaround CentOS error message during compilation PHP5 common (Linux)
- C language sorting instance (select, bubble, insert, binary, fast) (Programming)
- Ten correct use Redis skills (Database)
- Ubuntu 14.04 install the NVIDIA driver + CUDA + MATLAB (Linux)
- phpinfo (): like phpinfo as a Python script (Programming)
- Ora-00020: maximum number of processes (500) exceeded (Database)
- Eight kinds of techniques to solve hard problems Linux (Linux)
- Sublime Text 3 using summary (Linux)
- php security study (Linux)
- GitLab remote backup of Linux Shell Scripting (Linux)
- Sniffer Linux Environment (Linux)
- Build and verify MongoDB3.0.7 version (shard + replica) Cluster (Database)
- MySQL appears data too long for column 'name' at row 1 to solve (Database)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.