Tabactivity, localactivitymanager, tabhost, and tabwidget deep analysis

Source: Internet
Author: User

Tabacitivity inherits from acitivtygroup. The main function of acitivitygroup is to create a localactivitymanger, and then pass the oncreate event of the activity to localactivity for processing.

Tabactivity has three important parts: tabhost, tabwidget, and localactivitymanager.

Tabhost is a user-oriented interface. It is mainly used to add a tab and use tabspec to abstract a complete tab (including the tag and its content), a string-type tag, this tag is used to identify a tab. For example, when you exit the program, record which tab is currently used to display the tab displayed before exiting when you enter again, its most important role is to use intent as a tab, that is, to embed an activity as content (that is, embeded)
The concept of activity.

Another important part of tabhost is that it uses the policy pattern to abstract tags and content.

Create an indicatorstrategy interface and use createindicatorview () to create a view (that is, the label displayed on the tabwidget). According to the input parameters, there are three options: labelindicatorstrategy, strategy, and viewindicatorstrategy. From the name, we can see that a tag can contain only strings, strings, images, or custom views.

The contentstrategy interface is used to abstract the content. There are two methods: getcontentview () is used to obtain the view, and tabclosed () is used to close the operation (for example, the user clicks another tab and closes the current tab ), viewidcontentstrategy (a layout ID is given as the content) and factorycontentstrategy (the user inherits tabcontentfactory and uses createtabcontent () to create a view as the content ), intentcontentstrategy (specify an intent (that is, an activity as the content. The content rootview is a framelayout. The switchover is to make the original view
Invisible. Create a view when you click the tab at the beginning and use it directly later. Therefore, when actvity is used as the content, it may be dull if it takes a long time to create the tabhost (here, you may be able to do time-consuming operations first when creating the tabhost ), however, later switching is smooth.

Specifically, the activity content is started by using localactvitymanager, and a window is obtained, and then the decorview of the window is obtained to get the view, and then as the content. The Code is as follows:

Final window W = mlocalactivitymanager. startactivity (mtag, mintent );


Final view WD = W! = NULL? W. getdecorview (): NULL;


In the previous section, we analyzed tabhost, and then analyzed tabwidget.

Tabwidget is relatively simple. It inherits from linearlayout and is used to place labels. It overwrites addview (view child) to add a tag. When no view layoutparams is specified, the label height is filled with tabwidget by default, and the width of layoutparams is evenly divided by the number of labels, if layoutparams is not set when tags are added, the average Label Distribution is displayed. Then, determine whether to add dividerdrawable based on whether dividerdrawable is the image between labels. Tabwidget rules are relatively fixed and may not be very attractive when used. In fact, some people on the Internet say that using tabactivity is not as good as using Gallery, if you do not need to embed an activity as a view, using gallery is indeed more beautiful and dazzling, but it makes sense. I think the reason why tabactivity actually exists is for embeded.
Activity, that is, adding the activity as a view to another actvity, while localactivitymanger controls the lifecycle of the activity. Because the tabwidget layout is relatively fixed, it may not look beautiful. You need to customize your tabwidget as needed (for example, I add a gap in the tabwidget, to make it look like a tag is grouped), you need to pay attention to two functions when modifying: getchildtabviewat (INT index) and gettabcount (), because the view layout of the system itself is either all labels, or a dividerdrawable adds a label layout, the implementation of these two functions is relatively simple, if you customize your tabwidget, you may need to modify these two functions, which is a little complicated.

Localactivitymanager mainly refers to a function startactivity (string ID, intent). Because the string ID is used to identify an activity, the tab tag cannot be duplicated during addtab. When you click the activity tag in tabactivity, startactivity () is called to obtain the view, which is different from the flags of intent at startup, therefore, when setting intentflags, you must note that (The following paragraph is from personal translation. If your English level is poor or is incorrect, please forgive me) If you call startactivty () This ID
If an activity has already been started, it will be either destroy and re-created and started according to different situations, or it will be used directly. The original text and translation are as follows:

/**

* Start a new activity running in the group. Every activity you start

* Must have a unique string ID associated with it -- this is used to keep

* Track of the activity, so that if you later call startactivity () again

* On it the same activity object will be retained.

*

* Start a new activity running in the group. Each started actvity must have

* A unique string ID, which is used to record traces of the activity.

* When startactivity () is called again, the same activity object will be retained.

*

* <P> when there had previusly been an activity started under this ID,

* It may either be destroyed and a new one started, or the current

* One re-used, based on these conditions, in order: </P>

*

* If an activity has already been started under this ID, it may vary depending on the situation.

* It will either be destroyed and then start a new one, or the existing

* Re-use, in the following order:

* <Ul>

* <Li> If the intent maps to a different activity component than is

* Currently running, the current activity is finished and a new one

* Started.

*

* If intent is mapped to a different activity commp onent (package name) from the current running activity)

* The current activity is terminated and a new activity is started.

*

* <Li> if the current activity uses a non-multiple launch mode (such

* As singletop), or the intent has

* {@ Link intent # flag_activity_single_top} flag set, then the current

* Activity will remain running and its

* {@ Link activity # onnewintent (intent) activity. onnewintent ()} Method

* Called.

*

* If the current activity uses a non-Multi-run mode (such as singletop ),

* Or if intent sets flag_activity_single_top, the current activity

* It will continue to run and will call activity. onnewintent ().

*

* <Li> If the new intent is the same (excluding extras) as the previous

* One, and the new intent does not have

* {@ Link intent # flag_activity_clear_top} set, then the current activity

* Will remain running as-is.

* <Li> otherwise, the current activity will be finished and a new

* One started.

* </Ul>

*

* If the new intent (Extras not included) is exactly the same as the previous one and is not set

* Flag_activity_clear_top: The current actvity will continue to run.

* Otherwise, the current activity is terminated and a new activity is started.

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.