Android Learning Notes

Source: Internet
Author: User

1, Android's four components of the approximate function

Activity: Responsible for displaying the interface and interacting with the user.

Service: Running in the background.

Content provider: Provides an interface for data access between program apps.

Broadcast: Broadcast

2. Activation of components

Activity, service, broadcast are all excited by intent.

Start activity:

You can start an activity (or let it do some new work) by passing a intent to startactivity () or Startactivityforresult () (            When you want an activity to return a result for you). Intent Intent=NewIntent (xskpactivity. This, xsspmlactivity.class); Startactivityforresult (Intent, REQUEST_CXSPML);//jump from Loginactivity interface to homeactivity interfaceIntent Intent =NewIntent (); Intent.setclass (loginactivity. This, Homeactivity.class);//describe the starting point and targetStartActivity (Intent);
View Code

Start Service:

With StartService or Bindservice.

Start broadcast

With Sendbroadcast,sendorderedbroadcast, or sendstickybroadcast.

3. configuration file Androidmanifest.xml

All components: Activity,service,broadcast,contentprovider must be declared in this file.

Declaring a program to get permissions

Declarative configuration requirements for program uptime: Includes API requirements, configuration component requirements

4. Use of Activity 4.1, create activity

1) by creating a subclass of activity, in the OnCreate () method inside the class, the UI of the program activity is set by calling Setcontentview () and transferring the ID of the layout file XML.

2) The layout of the activity is implemented using an XML file, or you can write your own layout in the code.

3) Configure activity in the Androidmanifest.xml file, no access is not declared

<manifest >  <application ... >      <activity android:name=". Exampleactivity" /> ...   </application ... > ...   </manifest >
View Code

4.2. Start activity

1) simply start another activity in your own program

New Intent (This, signinactivity.  Class); startactivity (intent);
View Code

Write the activity name you want to activate in intent.

2) Activate the activity of other programs
New Intent (intent.action_send); Intent.putextra (Intent.extra_email, Recipientarray); StartActivity (Intent) ;
View Code

In intent, the conditions for activity to be started are described, and programs that meet these conditions start automatically, and if there are more than one program, the system will let the user choose which program to run.

4.3. Start an activity with a return result

1) just use the Startactivityforresult () method code startactivity ().

2) Implement the Onactivityresult () callback method in this activity to obtain the returned result.

Private voidpickcontact () {//Create an intent to ' pick ' a contact, as defined by the content provider URIIntent Intent =NewIntent (Intent.action_pick, Contacts.content_uri); Startactivityforresult (Intent, pick_contact_request);} @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {    //If The request went well (OK) and the request were pick_contact_request    if(ResultCode = = Activity.result_ok && Requestcode = =pick_contact_request) {        //Perform a query to the contact's content provider for the contact ' s namecursor cursor =getcontentresolver (). Query (Data.getdata (),NewString[] {contacts.display_name},NULL,NULL,NULL); if(Cursor.movetofirst ()) {//True If the cursor is not empty            intColumnIndex =Cursor.getcolumnindex (contacts.display_name); String name=cursor.getstring (columnindex); //Do something with the selected contact ' s name ...        }    }}
View Code

4.4. Close activity

Use Finish () or finishactivity (), but generally to close the activity, the user can not return to the activity after the shutdown, generally is the delivery system to close, the manual shutdown after the impact of the user's experience.

4.5. Manage the life cycle of activity

1) Life cycle is:

Resumed:activity at the front desk with the focus

Paused:activity was blocked by other activity, without focus, but it was visible.

Stop:activity is completely covered by other activity.

2) Implement life cycle callback method

 Public classExampleactivity extends Activity {@Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); //The activity is being created.} @Overrideprotected voidOnStart () {Super.onstart (); //The activity is about to become visible.} @Overrideprotected voidOnresume () {super.onresume (); //the activity has become visible (it's now "resumed").} @Overrideprotected voidOnPause () {super.onpause (); //Another activity is taking focus (the This activity is on to being "paused").} @Overrideprotected voidOnStop () {super.onstop (); //the activity is no longer visible (it's now "stopped")} @Overrideprotected voidOnDestroy () {Super.ondestroy (); //The activity is on to being destroyed.    }}
View Code

It is common to implement activity layouts and create resources in the OnCreate method, freeing resources in the OnDestroy () method.

3) Life cycle graphs and tables

Method Description can it be killed later? Next Method
OnCreate () Called when the activity is created for the first time. Here you should do all the usual static setup work-creating a view, binding the list data, and so on. This method passes in a bundle object that contains the previous state of the activity (if it was previously captured, see saving Activity State later).

The next callback method is always OnStart ().

Whether OnStart ()
Onrestart () Called before the activity is stopped and then started again.

The next callback method is always OnStart ()

Whether OnStart ()
OnStart () Called before the activity is to be displayed to the user.

If the activity enters the foreground, the next callback method is Onresume (), or the next callback method is OnStop () if it goes into a hidden state.

Whether Onresume () or onStop ()
Onresume () Called before the activity starts interacting with the user. The activity is at the top of the activity stack, where the user can enter it.

The next callback method is always OnPause ().

Whether OnPause ()
OnPause () Called when the system is ready to start another activity that is recovering. This method is commonly used to commit unsaved changes to permanent data, stop animation playback, and other tasks that may consume CPU, and so on. It should complete the work very quickly, because the next activity will not be resumed until the method returns.

If the activity returns to the foreground, the next callback method is Onresume () and if the user is not visible, the next one is OnStop ()

OK Onresume () or onStop ()
OnStop () Called when activity is no longer visible to the user. The reason may be that it is about to be destroyed, or that other activity (existing or new) is resumed and will overwrite the activity.

If the activity also comes back to interact with the user, the next callback method is Onrestart (); If the activity is about to disappear, the next callback method is OnDestroy ()

OK Onrestart () or OnDestroy ()
OnDestroy () Called before the activity is destroyed. This is the last call the activity received. It may be because the activity has finished working (some people call finish here), or it may be because the system temporarily destroys the activity's instance in order to make room for it. You can use the Isfinishing () method to differentiate between the two cases. OK No
4.6, Save the activity status (to learn) 4.7, multi-activity cooperation (to learn) 5, fragments (to learn) 6, stacks and back stack (to learn)

Android Learning Notes

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.