The four basic components of Android are activity, service, content provider, and broadcastreceiver.
This chapter mainly involves the following knowledge points:
I. lifecycle and application scenarios of an activity:
2. Change activity to a window: Activity attribute setting
3. What should I do if your background activity is recycled by the system: onsaveinstancestate?
Lifecycle of an activity:
The lifecycle of an android application is controlled in a unified manner. That is to say, the fate of the application we write is in the hands of others (systems). We cannot change it, but can only learn and adapt to it.
Simply put, Why is it like this: When a mobile phone is running an application, it is possible to call in and send a text message, or if there is no power, the program will be interrupted at this time, give priority to the basic functions of service calls,
In addition, the external system does not allow you to occupy too much resources. At least make sure that the phone function is used. Therefore, when resources are insufficient, the system can be killed.
Activity lifecycle Diagram
Four statuses:
Generally, an activity is in one of the following four States at a given time point:
Active: When the activity runs on the front-end of the screen, it obtains the focus to respond to the user's operations. The activity is in the running status, and only one activity is active or running at the same time.
Paused: When the activity loses focus but is still visible to the user (for example, if there is another transparent activity on it or a pop-up window such as toast and alertdialog), it is paused. The paused activity is still alive (it retains
Some status and member information, and keep the connection with the window manager), but the system can be killed when the system memory is very small.
Stopped: It is completely blocked by another activity and is in the stopped state. It retains all the status and member information. It is only invisible to users. When memory is needed elsewhere, it is often killed by the system, but it can communicate through notification.
Dead: The activity has not been started, has been manually terminated, or is not active when it has been recycled by the system. To manually terminate the activity, you can call the "finish" method in the program. For example, in order to release the scarce memory space.
Status transition events:
When the status changes, Android provides one or more call methods. We rewrite these methods to implement our processing.
Public class myactivity extends activity {
Protected voidOncreate(Bundle savedinstancestate ){..... }
Protected voidOnstart(){..... }
Protected voidOnrestart(){..... }
Protected voidOnresume(){..... }
Protected voidOnpause(){..... }
Protected voidOnstop(){..... }
Protected voidOndestroy(){..... }
}
Our activity will reload these methods as needed. oncreate is inevitable. During the normal startup of an activity, they are called in the order of oncreate-> onstart-> onresume,
When the activity is killed, the sequence is onpause-> onstop-> ondestroy, which is a complete life cycle,
But someone asked, What should I do if the program is running and there is a call? If a new activity is in full screen when it is aborted: onpause-> onstop, onstart-> onresume when it is restored, if the application is interrupted by an activity whose theme is translucent or dicent, it is only onpause. When it is restored, onresume
Seven States of lifecycle Resolution:
Oncreate (bundle savedinstancestate): usually used for initialization settings: 1. Set the layout file to be used for the activity; 2. Bind a listener to the button and other static settings
Onstart (): When the activity is visible and the user focus is not obtained, the system calls
Onrestart (): The system calls
Onresume (): The system calls
Onpause (): onpause () should be used to store persistent data.
When the system starts another activity, it is called by the system to save the persistent data in the existing activity and stop the animation before the new activity starts. This implementation method must be very fast. After the activity is disabled, the system does not recycle the memory. The user will expect that when he returns to this activity again, it will remain like the last time he left. At this time, onsaveinstancestate () is used. The onsaveinstancestate () method is used to save the status before the activity is killed and is triggered before onpause, this method needs to be rewritten when the system destroys the activity to save memory (the user does not want to destroy it). When the activity is instantiated again, it will be passed through oncreate (bundle
Savedinstancestate) transfers saved temporary State data to the instance because the onsaveinstancestate () method is not always called. The trigger condition is (press the Home Key and press the power button to close the screen, in the case of landscape switching), you should only rewrite onsaveinstancestate () to record the temporary status of the activity, rather than persistent data. Onpause () should be used to store persistent data.
Onstop (): called by the system when the activity is completely overwritten and invisible by the new activity
Ondestroy (): When the activity (the user calls finish () or the system is killed due to insufficient memory) is destroyed by the system, the system calls it (only one call is called throughout the lifecycle) to release oncreate () resources created in the method, such as the end thread
Example of a lifecycle scenario:
The following is an example of an example:
Scenario 1: If we start another activity (activity2) through intent in an activity (activity1), the triggering process is:
Activity1-onPause ()-> activity2-onCreate ()-> activity2-onStart ()-> activity2-onResume ()-> activity1-onStop ()
Activity1: it loses the user's focus and cannot interact with the user. It is overwritten by activity2, onpause () is called, and then the start call process of activity2. Usually activity2 is not covered, and occupy the screen completely, thus activity1 is hidden, activity1 enters the stopped state, calls the activity1-onStop (). If acvity2 is in a dialog box, instead of a full screen, that is, activity1 does not and therefore remains in the paused state, the activity1-onStop () is not triggered ().
Activity2: The Starting sequence of an activity is to initialize oncreate ()-> User-visible onstart ()-> User Interaction (Focus) onresume ()
Scenario 2: press the return key of the menu to exit from activity2 and return to activity1.
Activity2-onPause ()-> activity1-onRestart ()-> activity1-onStart ()-> activity1-onResume ()->
Activity2-onStop ()-> activity2-onDestory ()
Activity1: in scenario 1, it enters the stopped state, resumes, and enters the onrestart () state. The UI can be viewed as onstart (), and users with focus can interact with onresume ()
Activity2 () disabled.
2. Change activity to a window: Activity attribute setting
You only need to set the theme of the activity to define it in androidmanifest. XML, for example, a topic floating on the mobile phone's main interface.
XML Code
Android: theme = "@ Android: style/theme. Dialog"
Android: theme = "@ Android: style/theme. Dialog"
This makes your application pop up in the form of a dialog box, or
XML Code
Android: theme = "@ Android: style/theme. translucent"
Android: theme = "@ Android: style/theme. translucent"
It becomes translucent,
The above is the property name. The specific value is in Android. r. style, such as @ Android: style/theme. dialog "corresponds to Android. r. style. theme_dialog
3. What should I do if your background activity is recycled by the system: onsaveinstancestate?
The method for saving data is onsaveinstancestate (). Note that because the activity is executed frequently, we must ensure the efficiency and only store our data as bundle. There are two ways to obtain data: one is that the parameter in oncreate (bundle) carries the data, and the other is in onrestoreinstancestate (). We can select the desired data, which can even be a callback
When one activity a in your program is running, it actively or passively runs another activity B
At this time, a will execute
Java code
Public
Void onsaveinstancestate (bundle outstate ){
Super. onsaveinstancestate (outstate );
Outstate. putlong ("ID", 11111111111 );
}