Summary of Android's four major components, five major storages, and six major layouts





Android's four major components, five major storages, and six major layouts:

Reference link: https://www.cnblogs.com/tsingke/p/9007563.html



Four major components:

Activity (activity),
service (service),
content provider (Content Provider),
broadcast receiver (Broadcast Receive).

The active application is the display layer, each screen corresponds to your application service, the invisible workers in the service application, and the content refers to the shared data storage provided, the database used to manage and share the application, and the broadcast receiver Refers to the receiver of the broadcast.



①Activity (activity):

Activity is a component of an application that provides an area on the screen that allows users to do some interactive operations on it, such as making calls, taking pictures, sending emails, or displaying a map! Activity can be understood as a window that draws the user interface, and this window can fill the entire screen, or it may be smaller than the screen or float on top of other windows!
Activity is used to display user interface. The user completes related operations through the interaction of the Activity; an App application allows multiple Activities.

The concept of Activity and its life cycle diagram:

Guyu



Activity lifecycles mostly come in pairs:

onCreate() and onDestroy(), the beginning and end of life;
onStart() and onStop(), whether the interface is visible or not;
onResume() and onPause(), whether it can interact.

Detailed introduction:

onCreate():

The first method to call, usually load layout files, initialize resources, register events and BroadcastReceiver and other heavy tasks in this method.

onStart():

It is called after the onCreate() method. Usually, the display control logic of the UI is processed in this method. The task should not be too heavy. At this time, the user cannot interact or directly see the current Activity.

onResume():
Called after the onStart() method, usually simple data processing and UI control are performed in this method, such as starting animation, and only light tasks are performed. After the method is executed, the user can interact and directly see the current Activity. At this point, the current Activity enters the Resumed state (running state), and when an Activity in the Paused state is located at the top of the stack again (returning from other Activities), this method will be called again.

onPause():

Called when the current Activity is covered by other Activities, so that the current Activity enters the Paused state (suspended state), at this time the current Activity is visible but not interactive. Usually simple data processing and UI control are performed in this method, such as pausing animations, and only light tasks are performed. When there is insufficient memory and there are no other activities with lower priority, the current Activity may be destroyed. The onResume() method is called when the current Activity is back on top of the stack (returning from another Activity).

onStop():

Called when the current Activity is completely covered by other Activities (if the upper Activity is transparent or translucent, only the onPause() method is called), the current Activity enters the Stopped state (stopped state), and the current Activity is invisible. The display control logic of the UI is usually handled in this method, and the task should not be too heavy. When there is insufficient memory and there are no other activities with lower priority, the current Activity may be destroyed. When the current Activity is at the top of the stack again (returning from another Activity), the onRestart() method will be called.

onRestat():

When an Activity in the Stopped state is returned, this method is called, and then the onStart() method and the onResume() method are called to enter the running state.
onDestroy(): Called when the current Activity is destroyed, usually used in this method to perform heavy tasks such as releasing resources.



Activity creation process:
Guyu



There are several ways to start an Activity:
Guyu



Illustration of the four loading modes of Activity:
Guyu

Details description:

Standard: Standard mode, the default startup mode of Activity. In this mode, the Activity can have multiple instances. Every time the Activity is started, the system will create a new Activity instance regardless of whether the Activity instance already exists in the task stack.

SingleTop: the top of the stack mode, the mode of multiplexing the top Activity of the stack. When an Activity in SingleTop mode is already at the top of the stack, it will not create a new instance if it is started; if it is not at the top of the stack, the system will create a new Activity instance regardless of whether the Activity instance already exists in the task stack.

SingleTask: Single-task mode, a mode for multiplexing activities in the stack. If the started Activity already exists in the task stack, other activities above this Activity will be popped out of the stack, so that this Activity is at the top of the stack; otherwise, a new Activity instance will be created.

SingleInstance: single instance mode. In this mode, the system will start a new stack to manage the Activity, and the Activity can be shared between different applications.





②Service (service):

Concept:
Service is literally translated as "service", which is mainly used to process some time-consuming logic in the background, or perform some long-running tasks. When necessary, the service can even continue to run after the program exits.

Role:
Undertake most of the data processing work, provide heavy and time-consuming background services for other components, and monitor the running status of other components.

Service (service) life cycle diagram:
Guyu
use Service related methods in detail:

Guyu





③ContentProvider (content provider):

Concept:
ContentProvider is literally translated as "content provider", which is mainly used to share the data of this application with other applications. According to this feature, ContentProvider can also be used for inter-process communication. The application that provides data registers ContentProvider, and the application that obtains data creates ContentResolver to obtain the data corresponding to ContentProvider.

The role of
Content Provider is to provide the specified data set of an application to other applications. Other applications can obtain or store data from this content provider through the ContentResolver class. Developers usually do not use the objects of the ContentProvider class, and most of them implement operations on the ContentProvider through the ContentResolver object.



Guyu



Custom ContentProvider:
Guyu



Monitor the data change process of ContentProvider through ContentObserver:
Guyu





④BroadcastReceiver (broadcast receiver)

Concept:
BroadcastReceiver is literally translated as "broadcast receiver", which is mainly used to receive broadcasts from systems and applications. In the Android system, broadcasts are reflected in all aspects, such as boot broadcasts, network status broadcasts, battery power broadcasts, and so on.

Function:
Broadcasting can be used to process messages asynchronously. At this time, the function is similar to Handler. What is more powerful than Handler is that broadcasting can communicate messages within the entire device. Based on this feature, it can be used to solve the problem of inter-process communication.



Two broadcast types:
Guyu



There are two ways to register broadcasts:
Guyu







Five major storages:

SharedPreferences,
file storage,
SQLite database mode,
content provider (Content Provider),
network





Six layouts:

LinearLayout,
FrameLayout,
TableLayout,
RelativeLayout,
AbsoluteLayout,
GridLayout


①LinearLayout (linear layout):

Guyu

Linear layout is to arrange sub-elements (which can be controls or layouts) in sequence in horizontal or vertical order, and each element is located after the previous element. There are two types of linear layouts: horizontal and vertical. They are set by attributes android:orientation="vertical" and android:orientation="horizontal" respectively. android:layout_weight represents the proportion of the space occupied by the child elements.

Attributes:
   orientation: used to specify the arrangement direction of the current linear layout.
   horizontal: horizontal horizontal
   vertical: vertical vertical
   wrap_content: wrapping content
   match_parent: matching parent class
   margin: outer margin
   padding: inner margin
   gravity: indicates the alignment of the internal content of the control with respect to the control itself
   layout_gravity: indicates that the control itself is in the parent class layout The alignment
  
   layout_weight: Indicates the concept of weight, that is, the alignment in the form of the percentage:
      if the area divided by the control is wrap_content, the larger the weight value, the larger the proportion will be;
      if the area divided by the control is match_parent, then The larger the weight value, the smaller the proportion will be.
      
      Note: If the linear layout arrangement is horizontal, then layout_gravity will not work in the horizontal direction, but only in the vertical direction.

Display features: All sub-controls are arranged horizontally or vertically.

android:orientation="vertical" (vertical),
android:orientation="horizontal" (horizontal).

Commonly used attributes:
①gravity (the position of the center of gravity within the control itself)

Common values:
left (left), right (right), top (top), bottom (bottom), center (center),
enter_vertical (vertical center), center_horizontal (horizontal center)

Note that the attribute can also use android:gravity="bottom|center" to indicate the bottom center

②layout_gravity (the position of the center of gravity of the control in the parent container)
common values:
left (left), right (right), top (top), bottom (bottom), center (center),
enter_vertical (vertical center), center_horizontal (horizontal center )

③Usage of layout_weight (weight value)
sub-control: android:layout_weight="1" Multiple controls can be used at the same time to achieve the effect of equal division

④weightSum (weight sum)



②FrameLayout (frame layout is also called frame layout):

two attributes

andrid:foreground: Set the foreground image of the frame-changing layout container;
andrid:foregroundGravity: Set the display position of the foreground image.

Each component in the frame layout represents a screen, which can be used to achieve animation effects

1. By default, the arrangement starts from the upper left corner (0,0)
2. Each of the controls defined in the frame layout is presented in the form of a screen
3. The first defined control appears at the bottom, and the last defined control appears on top.

Note: Frame layouts can be used to display letters in the phone contacts' navigation to render frame layouts using in-frame animations.

Display features: All sub-controls are displayed in the upper left corner of FrameLayout by default, and will be displayed overlapping each other.

Common attributes:
layout_gravity (set to sub-controls, adjust the center of gravity of the control in the container)
common values:
left (left), right (right),
top (top), bottom (bottom),
center (center), center_vertical (vertical center)
center_horizontal (horizontal center)



③TableLayout (table layout):

Guyu

Attributes:

stretchColumns: Stretch a column to make the layout less compact
shrinkColumms: Retract a column so that the overall content can be presented
collapseColumns: Hide a column

TableLayout is a management control in the form of rows and columns, and each row has a TableRow object.
TableLayout does not generate borders

Note: The width and height of TableRow can not be specified, the system will automatically give the corresponding width and height

The table layout is similar to the grid layout, but it should be noted that the table layout cannot span rows, only columns



④RelativeLayout (relative layout):

Guyu

Arranged according to the relative position between spaces, there is a concept of reference.

Attributes:
centerInparent: Located in the middle of the parent class
alignParentRight: Align the right side of the parent class
alignParentLeft: Align the left side of the parent class
toRightOf: On the right side of a control
toLeftOf: On the left side of a control
alignBottom: Bottom alignment
alignTop: Top Alignment
alignRight: right alignment
alignLeft: left alignment
alignBaseLine: baseline alignment

Display features: Similar to the LinearLayout layout, all sub-controls are displayed in the upper left corner of the RelativeLayout by default

Common properties:

① On which side of the specified control: (Note: These attributes need to have a specified id)

layout_toRightOf is on the right side of the specified control
layout_toLeftOf is on the left side of the specified control
layout_above is above the specified control
layout_below is below the specified control sub-control alignment relationship

②Specify the alignment with that control (note: these properties need to have a specified id)

layout_alignRight aligns to the right of the specified control
layout_alignLeft aligns to the left of the specified control
layout_alignTop aligns to the top of the specified control

③The alignment relationship between the child control and the parent container (the value of these properties is true or false)

layout_centerInParent aligns to the middle of the parent container
layout_centerVertical aligns to the vertical center of the parent container
layout_centerHorizontal aligns to the horizontal center of the parent container
layout_alignParentLeft aligns to the left of the parent container layout_alignParentTop aligns to the
top of the parent container layout_alignParentRight aligns to
the right of the parent container
layout_alignParentBottom aligns to the bottom of the parent container



⑤AbsoluteLayout (absolute layout):

Also known as coordinate layout, you can directly specify the absolute position of child elements

Features: This layout is simple, direct, and highly intuitive; in use, Layout_x and Layout_y are generally used to specify the location where the corresponding controls are stored

Disadvantages: It is not conducive to the adaptation of the screen

Four major control attributes (units are dp):

Control size:

android:layout_width: component width;
android:layout_height: component height;

Control position:

android:layout_x: Set the X coordinate of the component;
android:layout_y: Set the Y coordinate of the component.



⑥GridLayout (grid layout):

Guyu

Attributes:

layout_columnSpan: The number of extended columns
layout_rowSpan: The number of extended rows
layout_gravity: Filling method (fill)
columnCount: How many columns exist
RowCount: How many rows exist

Note: What is the difference between GridLayout and TableLayout?
  TableLayout defines TableTRow to present content, and controls can be defined in GridLayout for direct use;  
  in table layoutcan onlyMerge columns, not rows, but you can merge columns and rows in a grid layout

Display features:
All sub-controls are arranged horizontally and sequentially in GridLayout by default. When only waiting for the number of columns in each row, when the specified number of columns is reached, the display will automatically wrap.

Common properties:

layout_column in the grid's column
layout_row in the grid's row
layout_columnSpan across the
column layout_rowSpan across the row
layout_gravity the center of gravity position in a grid
columnCount the total number of columns per row





  • It is recommended to use the six major layouts in Android:
    the weight property of LinearLayout (linear layout) + RelativeLayout (relative layout) to build our interface more perfect!


There are also some basic UI controls: text box (TextView), input box (EditText), button (Button)...














Note:
Likes, comments, and reprints are welcome. Please give the link to the original text in an obvious place on the article page
. Those who know, thank you for reading my article in the vast crowd.
Where is the signature without personality!
For details, please follow me
and continue to update

Scan to have a surprise!
© 2021 05 - Guyu.com | 【Copyright All Rights Reserved】

Guess you like

Origin blog.csdn.net/weixin_49770443/article/details/116936281