Android study concluded (a) - Activity concept, life cycle and start-up mode

concept

Activity is one of the four components of the Android, which may comprise a user interface component, and mainly used for user interaction. Activity in the end what is it that? According to my understanding is that Android provides an interface that allows users to click and slide a variety of operations, this is the Activity.

Life cycle

First to map the life cycle of an Activity:

Let us look at the meaning of each callback method:

  1. onCreate: representation Activity is being created, this is the first life-cycle methodology.
  2. onRestart: Activity is expressed restart. Under normal circumstances, when seen with the current Activity from non-visible, onRestart will be called.
  3. onStart: representation Activity is being started, already visible, but has not yet appeared in the foreground, can not interact with the user.
  4. onResume: Activity represents the already visible, and appears in the foreground.
  5. onPause: representation Activity is being stopped.
  6. onStop: Activity represents about to stop, you can do something a little heavyweight recycling efforts, can not be too time-consuming.
  7. onDestory: Activity represent about to be destroyed, this is the last callback Activity life cycle, we can do some recycling and final release of resources.

The next order of execution can be analyzed Activity lifecycle two examples:

The first example is our long-press the Home key to return to the desktop system, the order of the process Activity life cycle is executed: onPause-> onStop, then click on the icon to start the APP APP, the order of the process life cycle Activity performed: onRestart-> onStart-> onRsume.

The second example is to assume there are two Activity A and B, jump from A to B, the life cycle sequentially performed: A: onPause-> B: onCreate-> B: onStart-> B: onResume-> A: onStop, and then returns to A, the order: B: onPause-> A: onRestart-> A: onStart-> onResume-> B: onStop-> B: onDestory.

The above is the flow of execution Activity lifecycle of our normal circumstances, if the cause Activity was killed and re-created, life cycle is kind of how in an abnormal situation? We look at the chart:

When Activity is terminated in exceptional cases, the system will call onSaveInstanceStat to save the current state of the Activity. Calling this method has the potential to be called before onPause, but also can be called after it.

When Activity is re-created, the system will call onRestoreInstanceState, and the time Activity destroy onSaveInstanceState method saved Bundle object passed as a parameter to onRestoreInstanceState and colleagues onCreate method. 

Start mode

Pattern Classification

  1. standard: Standard mode, which is the default mode. Each time you start an Activity will re-create a new instance, regardless of whether this instance already exists.
  2. singleTop: stack multiplexed mode. If you want to start Activity is already in the top of the stack task stack, then the Activity will not be re-created, will reuse the current Activity top of the stack.
  3. sinleTask: the stack multiplexing mode. This is a single-instance mode, in this mode, as long as there is an Activity in a task stack, then start the Activity will not recreate instance, will only make the task stack is located in this Activity Other Activity above all removed stack, so this Activity top of the stack.
  4. singleInstance: singleton pattern. When starting a new Activity, the system creates a new task stack for it, then this Activity in the new independent task stack.

 

 

Scenarios

Standard Mode Needless to say, we say that the other three modes

  1. singleTop: Start page for notifications. For example, some of the push message, receiving a plurality of push, each time a new page is opened unfriendly, it is possible to use the startup mode.
  2. singleTask: as the entry point for the program. For example, the browser's home page, regardless of the number of applications from launching the browser, it will only start once the main page.
  3. sngleInstance: the need for the page program and separated. For example, alarm reminders.
Released four original articles · won praise 3 · views 10000 +

Guess you like

Origin blog.csdn.net/lsh_sh/article/details/86551859
Recommended