Activity gives us a window to interact with. When the activity class creates a window, the developer can place the UI on the activity-created window through the Setcontentview (View) interface.
When developing an application, we need to inherit the activity or its subclasses.
life cycle of activity:
The life cycle of the activity has been clearly demonstrated, and we have combined some more explanations.
full life cycle: OnCreate ()->ondestroy ()
Initializes the global resources and states that the activity can use in OnCreate (), releasing the resources in OnDestroy ().
visual life cycle: OnStart ()->onstop () and Onrestart ()
This process is the process of seeing the interface from visible to invisible, but not all the way is allowed to interact. OnStart () is typically used to initialize or initiate resources related to interface updates, and onStop () is typically used to pause and stop threads, timers, and services associated with the update interface.
activity life cycle: Onresume ()->onpause ()
The activity is at the top level of the screen and can interact with the user. The two functions of the cycle are frequently used, so the two methods should be as simple and efficient as possible.
Two other methods:onrestoreinstancestate () and onsaveinstancestate ()
An event callback function that does not belong to the life cycle, but can be used to save and restore the activity's bounds? Temporary information
Onsaveinstancestate () will save the temporary information in the bundle, OnCreate () and onrestoreinstancestate () can recover the saved information.
The simplified approach is to restore the onCreate (), but in some special cases it is necessary to restore the Onrestoreinstancestate () function:
Must be in bounds before the operation can be entered after the full initialization is completed
or need to be determined by the class to determine whether the default settings, etc.
configuration of activity:The four components are required to be explicitly configured. Activity is added to the <application.../> inside a <activity.../>.
Let's focus on a few of the properties that are commonly used during activity configuration:
Name: Specifies the class name of the implementation class for the activity.
Icon: Icons.
Label: Label.
Exported: Whether it is allowed to be called by another application.
Launchmode: Start mode, selectable with standard, singletop, Singletask, SingleInstance.
The following highlightsactivation mode of activity (Launchmode)。
Used to think that activity has an activity stack, the activity is so filo, the original is too na?ve.
Standard: Each time the activity is started, a new instance will be generated and added to the stack.
Singletop: Similar to standard, the difference is that when the active activity is at the top of the stack, no new instances are generated, but instead the instance of the stack is directly reused.
Singletask: There is the same approach as in the previous two, except that when the activity to be started is not at the top of the stack but is in the stack, all instances above the activity instance are moved out and the activity that needs to be started is placed on top of the stack.
SingleInstance: Each time a new activity is started, an instance is created and placed in a new stack, and each time the activity is started, the existing instance is reused directly.
activity's Jump:Start ActivitY methods are: (first say explicit start, implicit startup will be introduced later)
StartActivity (Intent Intent) and Startactivityforresult (Intent intent,int requestcode)
The second parameter of the second method, Requestcode, is the request code, which identifies the source of the start request.
Remember to configure intent in the configuration file, although most of the time we do not need to do it ourselves.
Close ActivityMethod of:
Finish () and finishactivity (int requestcode)
These two methods correspond to the previous two initiator method respectively.
We often use the following function before the method of closing activity is called:
void Setresult (int resultcode, Intent data)
Parameters:
Result code ResultCode: indicated? The return state of the Activity, usually ACTIVITY.RESULT_OK or activity.result_canceled, or other integral types that you define yourself.
Return value data:? The activity needs to return the data passed.
When the child activity is closed, we can override the public void Onactivityresult (int requestcode, int resultcode, Intent data) in the parent activity method to accept the request code, return status, and data for the child activity.
Data transfer between activity: Intent acts as a messenger to pass data to us, we just need to put the data needed to be sent to intent on the good. Intent how to access data:
Putextras (Bundle data)
Bundle Getextras ()
PutExtra (String key,xxx value): In fact, a bundle is implicitly generated.
Xxx Getxxxextra (String key)
Bundles can be understood as a packet that is used to load a wide variety of data, except here, when we use a message to pass data on different threads of the same activity, we also use bundles.
Ways to access data by bundle:
Putxxx (String key,xxx data)
GetXxx (String key)
Putserializable (String key,serializable data): Serializable refers to a serializable object, such as an instance of a class.
Getserializable (String key)
Implicitly launch activity scenarios such as you want to open a document, but your phone has one or several applications that can open the document, then the system will automatically match you to open the document application, you choose one to open the document, is to start an activity implicitly.
The application itself does not specify to enable that activity, but is determined by the system. This is a good way to reduce the coupling between components and improve the reusability of the components.
The system automatically matches the appropriate activity, and the matching mechanism is based on the Intent filter (Intent filter) in the Android system.
This part of the content is more complex, our next blog is introduced separately.
Android Learning notes: Activity