A brief introduction to the basic concepts of activity controls in Android development _android

Source: Internet
Author: User

Activity is the most basic module, commonly referred to as "activities", in an application, a function is usually a separate screen. Simply understood, the activity represents a screen that a user can see, primarily to handle the overall work of the application, such as listening to system events, displaying the specified view for the user, initiating other activities, and so on. All application activity is inherited from the Android.app.Activity class, which is the base class provided by Android, and the parent class is the parent class to implement the various functions.

The Activity life cycle chart is as follows:

In Android, activity has four basic states:

1, active/runing a new activity launched into the stack, it in the front of the screen, at the top of the stack, at this time it is visible and can interact with the user's activation state.

2. The state of paused when an activity is overwritten by another transparent or Dialog-style activity. It still remains connected to the window manager, and the system continues to maintain its internal state, so it is still visible, but it has lost focus and cannot interact with the user.

3. stoped is in stoped state when activity is covered by another activity and the loss of focus is not visible.

4, killed activity is killed by the system or is not started in the killed state.

When an instance of an activity is created, destroyed, or another activity is started, it transitions between these four states, which depend on the actions of the user program.

As shown above, an Android programmer can determine the "life" of an activity, but not the "death" of it, and then say that the programmer can start an activity, but cannot manually "end" an activity. When you call the Activity.finish () method, the result is the same as when the user presses the back key: Tell the activity Manager that the activities instance completes the work and can be "recycled". The activity Manager then activates and then paused the second layer of the stack, while the original is pushed into the second layer of the stack, from the active state to the state of the heap. For example: Activity2 is started from Activity1, then the top of the stack is Activity2, and the second layer is Activity1, and when we call the Activity2.finish () method, the activity Manager is reactivated The Activity1 is merged into the stack, Activity2 transforms the stoped state from the active state, Activity1. The Onactivityresult (int requestcode, int resultcode, Intent data) method is executed, and the data returned by the Activity2 is returned to the Activity1 by the parameter.

Non-user behavior when the activity is not visible, such as when the phone comes suddenly = =

/**
  * Re-create recovery cache Data * * *
  @Override
  protected void onrestoreinstancestate (Bundle savedinstancestate) {
    log.i ("Onrestoreinstancestate", Savedinstancestate.getstring ("name"));
    Super.onrestoreinstancestate (savedinstancestate);
  }
  /**
  * Some data to save the cache before it is destroyed * * *
  @Override
  protected void onsaveinstancestate (Bundle outstate) {
    Outstate.putstring ("name", "Concise Modern Magic");
    Super.onsaveinstancestate (outstate);
  }

In Android, there are 4 activation modes for activity, respectively:

    1. Standard: Standard mode, a call to the StartActivity () method produces a new instance.
    2. Singletop: If an instance already exists at the top of the activity stack, no new instances are generated, but only the newinstance () method in the activity is invoked. If not at the top of the stack, a new instance is generated.
    3. Singletask: this instance will be generated in a new task, which will be used every time in the call, and will not produce a new instance.
    4. SingleInstance: This is basically the same as Singletask, there is only one difference: In this model of the activity instance in the task, there can be only this activity instance, no other instances.

These boot modes can be set in the feature manifest file Androidmanifest.xml, in the Launchmode property.

There are also some flags in the relevant code that can be used, for example, if we want to enable only one instance, we can use the INTENT.FLAG_ACTIVITY_REORDER_TO_FRONT flag, which says: If this activity has been started, There will be no new activity, but just add the activity instance to the top of the stack.

Intent Intent = new Intent (reorderfour.this, reordertwo.class);

Intent.addflags (Intent.flag_activity_reorder_to_front);

StartActivity (Intent);

The load mode of the activity is controlled interactively by the attribute values of the elements of the activity in the flag and manifest files set in the intent object of the initiating activity.

Here are some features that affect the load mode

The core intent flag are:

    • Flag_activity_new_task
    • Flag_activity_clear_top
    • flag_activity_reset_task_if_needed
    • Flag_activity_single_top

The core features are:

    • Taskaffinity
    • Launchmode
    • Allowtaskreparenting
    • Cleartaskonlaunch
    • Alwaysretaintaskstate
    • Finishontasklaunch

The difference between the four load modes

The difference between the owning task

In general, the target task of the activity of "standard" and "Singletop", and the sender of the received intent in the same task, is equivalent to who calls it, and who is in the same task.

Unless intent includes parameter flag_activity_new_task. If the Flag_activity_new_task parameter is provided, it is launched into a different task.

"Singletask" and "singleinstance" always take the activity to start as the root element of a task, and they will not be launched into a different task.

Whether multiple instances are allowed

"Standard" and "singletop" can be instantiated multiple times and can exist in different tasks, where a task can include multiple instances of an activity;

"Singletask" and "singleinstance" restrict the generation of only one instance and are the root element of a task.

Singletop requires that if a intent is created with an instance of an activity to be created at the top of the stack, the intent is sent to the instance without creating a new instance.

Whether other activity is allowed to exist within this task

"SingleInstance" exclusive of a task, other activity can not exist in that task;

If it starts a new activity, regardless of the launch mode of the new activity, the new activity will run into another task (as with the Flag_activity_new_task argument).

The other three modes can coexist with other activity.

Whether new instances are generated each time

"Standard" will generate a new instance of activity for each startup intent;

Activity of "singletop" if it is at the top of the task stack, then the instance of the top of the stack is not generated, and the instance of the activity is generated directly.

Like what:

Now the task stack element is a-b-c-d (d at the top of the stack), which gives D a startup intent, if D is "standard", then a new instance of D is generated, and the stack becomes a-b-c-d-d.

If D is singletop, then a new instance of D is not produced and the stack state is still a-b-c-d

If you give B intent then, regardless of b launchmode is "standard" or "singletop", will generate a new instance of B, stack state into A-b-c-d-b.

"SingleInstance" is the only activity on its stack, which is reused every time.

If the "Singletask" is on the top of the stack, the intent is accepted, otherwise the intent is discarded, but the task will still return to the foreground. When an existing activity instance processes a new intent, the Onnewintent () method is invoked, and if the intent generates an activity instance, the user can return to the previous state via the back key; If it is an activity that already exists to handle this intent, the user cannot return to the previous state by pressing the back key.

Related Article

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.