Activity of Android four components

Source: Internet
Author: User

The four components of Android are "Activity", "broadcast receivers", "Service", "Content Providers ”

Activity is a UI component that interacts directly with the user, and the activity component undoubtedly occupies an important place in the Android UI component. In general, activity appears as a full-screen window, or as a floating window or other activity's child activity.

As one of the most important components of the application lifecycle, the release and management of activity forms the basis of the platform Application model. In Android, activity is managed through the activity stack, where there are 4 states in the activity's life cycle: Active (Active), run (running), stop (Stopped), pause (paused).

active/runing

When activity is in the front of the screen, activity is in the "active" or "running" state, at which point it is visible and can interact with the user's active state, called the active or the running state (active or running).

Paused

Activity is "paused" when activity loses focus but is visible (creates a new non-full-screen or transparent activity that is on top of the activity stack). Activity in the "Paused" state maintains all of its state and member information and continues to be under the management of the window manager, where activity in the "paused" state is likely to be killed in the event of an extremely low memory condition.

stoped

If an activity is completely covered by another activity, it is called a Stop state (Stopped). It still maintains all state and member information, but it is no longer visible, so its window is hidden, and when the system memory needs to be used elsewhere, stopped's activity will be forcibly terminated.

Killed

If an activity is a paused or stopped state, the system can remove the activity from memory, and the Android system deletes it in two ways, either requiring the activity to end or terminating its process directly. When the activity is displayed again to the user, it must restart and reset the previous state.

When the activity becomes visible again, if you need to do a refresh on the interface, the associated action can be handled in the activity:: Onresume () method. The focus will be given after the activity is built, and the corresponding focus changes can be monitored in the activity:: Onwindowfocuschanged () method.

Activity StackAndroid manages activity through an activity stack, and the state of an activity instance determines its position in the stack. The activity at the foreground is always at the top of the stack, and the activity on the second level of the current station is activated and floats to the top of the stack when it is destroyed by an exception or other reason. When the new activity is launched into the stack, the original activity is pushed into the second layer of the stack. A change in the position of an Activity in the stack reflects its transformation between different states.

Switching between activity and activity often requires the help of intent components. One of the simplest ways to invoke this is as follows:

New Intent (context, bluetoothoppbterroractivity.  Class//
In.setflags (intent.flag_activity_new_task); // set Flag bit
// Set Title
In.putextra ("Content", context.getstring (R.string.unknown_file_desc)); // Setting display content
Context.startactivity (in); // start activity without returning data


In the Activty, the method of processing the data is as follows:

protected void onCreate (Bundle savedinstancestate) {        super. OnCreate (savedinstancestate);         = getintent ();         = Intent.getstringextra ("title");         = Intent.getstringextra ("content");    }

When you generalize an activity class, you must implement the activity:: OnCreate () and activity:: OnPause () two methods. When another activity is invoked through one activity, it can be done through the context.startactivity () method if it is not necessary to return data. If you need to return data, it is handled through the Context.startactivityforresult () method, which is common in the following ways:

New Intent (); Intent.setclass (this, createplaylist. class  //  new_playlist for "Requestcode"

In the tuned activity, after the request has been processed, you can return data to the calling activity in the following ways:

Setresult (RESULT_OK, (New Intent ()). SetData (URI));


For the returned data, the calling activity is processed in its Onactivityresult () method. The process is as follows:

  protected void onactivityresult (intint  resultcode, Intent Intent) {        switch (requestcode) {            case  new_playlist:            if (ResultCode = = RESULT _OK) {                = intent.getdata ();}}    }

The main activity properties are: Taskaffinity, Launchmode, allowtaskreparenting, Cleartaskonlaunch, alwaysretaintaskstate Finishontasklaunce and so on.

In addition, for some special activity, you may want to provide the title bar customization, in Android, the title bar supports the following features: Feature_no_title, feature_progress, Feature_left_icon, Feature_right_icon, Feature_custom_title and so on.

In order to hide the title bar, it can be implemented in Java code, can also be implemented in the Androidmanifest.xml file, due to factors on the framework, if implemented through Java code, the title bar in activity initialization still has a brief visibility, implemented in the Androidmanifest.xml file, there is no similar problem.

Its Java implementation is as follows:

 Public void onCreate (Bundle savedinstancestate) {
Super . OnCreate (savedinstancestate);
requestwindowfeature (window.feature_no_title);
// Java code mode
// must be placed behind Requestwindowfeature ()

}


The Androidmanifest.xml file is implemented in the following manner:

   <ActivityAndroid:name= ". Graphics. Framebufferobjectactivity "Android:label= "Graphics/opengl es/frame Buffer Object"Android:theme= "@android: Style/theme.notitlebar"android:configchanges= "Orientation|keyboardhidden">//Disable interface rotation<Intent-filter>          <ActionAndroid:name= "Android.intent.action.MAIN" />          <categoryAndroid:name= "Android.intent.category.SAMPLE_CODE" />        </intent-filter>    </activity>


If you want to customize the title bar. The method is:

 Public void onCreate (Bundle savedinstancestate) {    super. OnCreate (savedinstancestate);    Requestwindowfeature (window.feature_custom_title);    Setcontentview (r.layout.main);    GetWindow (). Setfeatureint (Window.feature_custom_title, r.layout.custom_title);    }


If you want to hide both the title bar and the status bar, here's how:

 <activity
Android:name= "Gl2jniactivity"Android:theme= "@android: Style/theme.notitlebar.fullscreen"//Hide status bar at the same time Android:launchmode= "Singletask"android:configchanges= "Orientation|keyboardhidden"> <Intent-filter> <ActionAndroid:name= "Android.intent.action.MAIN" /> <categoryAndroid:name= "Android.intent.category.LAUNCHER" /> </intent-filter></activity>

Response time of activity
The current activity is on the main thread, with a response time of 5 seconds, and the program will report a ANR error if it takes a time-consuming operation in the currently running activity and has a response time of 5 seconds. So, this is one of the reasons why it is not recommended to write too much complex code in activity.
Of course, some code can only be written in the activity, otherwise it will not work (they are not life cycle methods), such as you want to get the Android system or hardware information, you have to write in the activity, if you write a tool class alone can not get.

Resources:

Baidu Encyclopedia. Http://www.3g-edu.org/news/art086.htm

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.