Activity of Android four components

Source: Internet
Author: User

Android developers do not know the activity, activity is the most frequent developers encounter, the most components. Here are some of the more infrequently used knowledge to make a summary.

Activity life cycle

Referencing block content

Points

    • OnStart (), Onresume () is visible, onPause () is partially visible, and other is not visible
    • Onsaveinstancestate save state; Onrestoreinstancestate Restore State
Activity startup mode

There are four ways to activate the activity, namely:

    • Standard
    • Singletop
    • Singletask
    • SingleInstance

Standard: Default mode, can not write configuration. In this mode, a new instance will be created by default. Therefore, in this mode, you can have multiple identical instances, and also allow multiple identical activity overlays.

singletop: There can be multiple instances, but multiple identical activity overlays are not allowed. That is, if the activity is at the top of the stack, the same activity is started, the new instance is not created, and its Onnewintent method is called.

For example: If I have two activity named B1,B2, two activity content function is identical, there are two buttons can jump to B1 or B2, the only difference is B1 standard,b2 for Singletop.
If the order in which I intend to open is b1->b2->b2, then the actual order of opening is B1->B2 (the next attempt to open B2, which actually calls only the previous Onnewintent method)
If the order in which I intend to open is b1->b2->b1->b2, then the actual order of opening is the same as the intention, b1->b2->b1->b2.

Singletask: There is only one instance. When you launch him in the same application, if the activity does not exist, a new instance is created in the current task and, if present, the other activity on top of the task is destory off and the Onnewintent method is called. If you start it in another application, a new task is created and started in that task Activity,singletask allows other activity to be stored in a task, that is, If I open a new activity in this Singletask instance, the new activity will still be in the task of the Singletask instance.

For example:
If there are three activity,c1,c2,c3 in my application, three activity can start each other, C2 is Singletask mode, then no matter how I click Start in this program, such as: c1->c2->c3->c2- There may be multiple instances of >C3->C1-C2,C1,C3, but there is only one C2, and the three activity is in the same task.
But C1->C2->C3->C2->C3->C1-C2, this process should actually be as follows, because Singletask will destory the other activity on top of the task.
Operation: c1->c2 c1->c2->c3 c1->c2->c3->c2 c1->c2->c3->c2->c3->c1 C1->C2->C3-> C2->c3->c1-c2
Actual: C1->c2 c1->c2->c3 c1->c2 c1->c2->c3->c1 c1->c2

If another application opens C2, it will start a new task.
If the other application has a activity,taskid of 200, from which you open C2, the Taskidi of C2 will not be 200, for example C2 TaskID is 201, then C2, C1 from C3, C2 C3, TaskID is still 201.
Note: If you click Home and then open other, you'll see that it will certainly be the content in the other app, not one of the C1 C2 C3 in our app.

singleinstance: There is only one instance, and this instance runs independently in a task, and this task is the only instance that does not allow any other activity to exist.

For example:
The program has three activityd1,d2,d3, three activity can start each other, wherein D2 is singleinstance mode. Then the program starts running from D1, assuming that the taskid of D1 is 200, then D2 starts a new task when D2 is started from D1, that is, D2 and D1 are not running in a task. Assuming that D2 has a taskid of 201, and then starts D3 from D2, D3 TaskID is 200, which means it is pressed into the D1-initiated task stack.

If you open D2 in another application, assuming that other's taskid is 200, open d2,d2 will create a new task run, assuming its taskid is 201, then if you start D2 or D1 from D3, then another task is created, so If the operation step is OTHER->D2->D1, the process involves 3 tasks.

Activity Pass Parameters

Intent pass-through parameter types:
boolean, char, byte, double,float,int, long, short basic data types and their arrays
charsequence/charsequence[], String/string[], Bundle
and objects that implement Serializable or parcelable

Serializable: Java provides, all classes that implement Serializable, are serializable classes. You do not need to implement any methods.
parcelable:android Unique, high efficiency. Need to rewrite writetoparcel ()

Principles for choosing a serialization method

    1. When using memory,parcelable is better than Serializable , so it is recommended to use parcelable.

    2. Serializable generates a large number of temporary variables at serialization time, resulting in frequent GC.

    3. parcelable cannot be used in situations where data is stored on disk, because parcelable does not guarantee the continuity of the data in the event of external change. Although Serializable is inefficient, it is recommended to use Serializable at this point.

Implementing parcelable Steps

    1. Implements Parcelable

    2. Override the Writetoparcel method to serialize your object to a parcel object, that is, to write the data of the class to an externally supplied parcel, to package the data that needs to be passed to the parcel container to save the data from the parcel container

    3. Override describecontents method, content interface description, default return 0 can

    4. Instantiate a static internal object Creator Implement Interface Parcelable.creator

For example:

 Public  class Mydemo implements parcelable {    Private intMdata;Private intMhaha; Public int describecontents() {return 0; } Public void Writetoparcel(Parcel out,intFlags) {Out.writeint (mdata);    Out.writeint (Mhaha); } Public Static FinalParcelable.creator<mydemo> Creator =NewParcelable.creator<mydemo> () { PublicMydemoCreatefromparcel(Parcel in) {return NewMydemo (in); } PublicMydemo[]NewArray(intSize) {return NewMydemo[size]; }    };Private Mydemo(Parcel in)        {mdata = In.readint ();    Mhaha = In.readint (); }}
Acivity Configchanges Properties

The android:configchanges = "orientation|screensize" configuration is not commonly used in androidmanifest.xml files, and is of course useful.
If we configure this property, we will call the onconfigurationchanged method in the onCreate method without re-executing the onCreate method When we switch between the screen and the other. Of course, if you don't configure this property, the onCreate method will be called again.

android:configchanges=["MCC", "MNC", "locale",
"touchscreen", "keyboard", "keyboardhidden",
"navigation", "screenlayout", "fontscale", "uimode",
"orientation", "screensize", "smallestscreensize"]

  1. MCC: The IMSI Mobile Country Code (MCC) has changed-a SIM have been detected and updated the MCC.
    IMSI (International Mobile Subscriber ID) has changed, SIM card detected, or MCC updated
  2. MNC: The IMSI Mobile Network Code (MNC) has changed-a SIM have been detected and updated the MNC.
    IMSI network changes, SIM card detected, or MCC updated
    Where MCC and MNC are theoretically unlikely to change
  3. locale: The locale has changed-the user have selected a new language that text should is displayed in.
    The language changes, the user chooses a new language, the text should be displayed again
  4. Touchscreen: The touchscreen has changed. (This should never normally happen.)
    The touchscreen has changed, which is usually not supposed to happen
  5. Keyboard: The keyboard type has changed-for example, the user had plugged in an external keyboard.
    The keyboard type has changed, for example, the user has used an external keyboard
  6. **keyboardhidden:**the keyboard accessibility have changed-for example, the user has revealed the hardware keyboard.
    Keyboard changes, for example, the user uses a hardware keyboard
    Navigation: The navigation type (TRACKBALL/DPAD) has changed. (This should never normally happen.)
    Navigation changes, (which should not normally occur) Example: Connect a Bluetooth keyboard, which does cause the type of navigation to change. Because after connecting the Bluetooth keyboard, I can use the arrow keys to navigate the
  7. screenlayout: The screen layout has changed-this might is caused by a different display being activated.
    The layout of the screen changes, which can cause different displays to be activated
  8. Fontscale: The font scaling factor has changed-the user have selected a new global font size.
    Global font size scaling has changed
  9. Orientation: The screen orientation have changed-that is and the user has rotated the device. Equipment rotation, landscape display and vertical display mode switching.
  10. screensize: The screen size has changed
  11. smallestscreensize: The physical size of the screen has changed, such as: Connect to an external screen
  12. layoutdirection Property (), this property will also become a mask bit in newconfig when the language setting is changed. So Activitymanagerservice (actually in activitystack) always judges the restart when deciding whether to restart the activity.
    You need to add both locale and layoutdirection in Android:configchanges.
    Switch to Settings without exiting the app to switch languages and find the activity or restart.
Activity Windowsoftinputmode

Windowsoftinputmode is to set the interactive mode of the window soft keyboard, the property has a total of 9 values:

stateunspecified,stateunchanged,statehidden,statealwayshidden, Statevisible,statealwaysvisible,adjustunspecified,adjustresize, Adjustpan.

  1. stateunspecified
    When we do not set the Android:windowsoftinputmode property, the software uses this interaction mode, the system will take the corresponding soft keyboard display mode according to the interface, for example, When there is only text and buttons on the interface, the soft keyboard does not eject automatically because there is no need to enter it. Then, when the interface to get the focus of the input box, when the setting property is Stateunspecified, the system does not eject the soft keyboard, but when the focus of the input box interface has the need to scroll, will automatically eject the soft keyboard. As for why you should emphasize the input box to get the focus, this is because, if the input box is not the focus, the soft keyboard will not automatically pop up, so that the interface does not automatically eject the soft keyboard one of the solutions, is in the XML file, set a non-input box control to get focus, thereby preventing the keyboard pop-up.

  2. stateunchanged
    The meaning of the Chinese means that the state does not change, how should we understand this sentence? In fact, it is very well understood, that is, the current interface of the soft keyboard state, depending on the previous interface of the soft keyboard state. For example, if the current interface keyboard is hidden, then the interface after the jump, the soft keyboard is also hidden, if the current interface is displayed, then the interface after the jump, soft keyboard is also the display state.

  3. Statehidden
    As the name implies, if we set this property, then the keyboard state must be hidden, regardless of the previous interface what state, regardless of the current interface has no input requirements, anyway is not displayed. Therefore, we can set this property to control the soft keyboard does not automatically eject.

  4. Statealwayshidden
    When the Activity main window gets focus, the soft keyboard is always hidden.

  5. statevisible
    Set to this property, you can summon the soft keyboard, even if there is no input box on the interface can be forced to summon out.

  6. statealwaysvisible
    This property can also summon the keyboard, but it is slightly different from the statevisible attribute. For example, when we set the Statevisible property, if the current interface keyboard is displayed, when we click the button to jump to the next interface, the soft keyboard will be hidden because the input box loses focus, when we return to the current interface, the keyboard this time is hidden. But if we set the statealwaysvisible, we jump to the next interface, the soft keyboard is still hidden, but when we come back again, the soft keyboard will be displayed. Therefore, this always explains the difference, regardless of the situation to reach the current interface (normal jump or the previous interface is returned by the user), the soft keyboard is displayed state.
    Speaking of which, I think of the above Statehidden and Statealwayshidden, I guess the difference is the same, that is, Statealwayshidden is hidden anyway, but if you jump to the next interface, the soft keyboard is called out , then when the next interface is returned by the user, the keyboard should not be hidden, but I have not found a way to jump to the next interface, but also let the current interface soft keyboard does not disappear, so temporarily can not be verified.

  7. adjustunspecified
    Starting with this property, instead of setting the display and hiding mode of the soft keyboard, you set the display relationship between the soft keyboard and the software's display. This option is also the default setting mode when you are not setting this value with us. In this case, the system chooses different modes depending on the interface. If there are controls in the interface that can be scrolled, such as Scrowview, the system will reduce the size of the interface that can be scrolled, ensuring that even if the soft keyboard is displayed, all the content can be seen. If there are no scrolling controls in the layout, the soft keyboard may cover some of the content.

  8. adjustresize
    This property indicates that the activity's main window is always resized to ensure that the soft keyboard displays space.

  9. Adjustpan
    The contents of the current window are automatically moved so that the current focus is never covered by the keyboard and the user can always see the part of the input

Detailed reference: http://blog.163.com/[email protected]/blog/static/110674863201572513818601/

Activity Toggle Animation
    • Code
overridePendingTransition(R.anim.scale, R.anim.alpha)
    • XML file
 <style name="Animation_Activity" parent="@android:style/Animation.Activity">        <item name="android:activityOpenEnterAnimation">@anim/right_in</item>        <item name="android:activityOpenExitAnimation">@anim/left_out</item>        <item name="android:activityCloseEnterAnimation">@anim/left_in</item>        <item name="android:activityCloseExitAnimation">@anim/right_out</item>    </style>

Activity of Android four components

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.