Android Learning Note 8--user interface activty

Source: Internet
Author: User

Acitivity is an application component that responds to user interface, and is one of the four components of Android. Each activity is given a window to display the user interface, which is usually filled with the entire screen, or smaller than the screen, and displayed on top of other windows.

An application typically contains multiple interrelated activity, and one of the activity is called Main, which is the portal to the application, which is what we saw the first time we opened the app. To show different actions, each activity can start another activity. The start of each new activity represents the stop of the previous activity, but the system keeps the activity in a fallback stack called the ' Back stack '. The start of a new activity pushes the activity into the fallback stack and gets the user focus. This fallback stack follows the ' advanced out ' principle, so when the user is manipulating the current activity, the activity is pushed into the stack, and when the user presses the fallback button it will be popped up and the previous activity is back to normal.

When a new activity starts, another activity stops, and the inactive activity notifies the state of the change through the activity's lifecycle callback method. Because of changes in the state (whether the system is created, stopped, restored, destroyed), an activity may receive several callback methods, each of which gives you the opportunity to do some special work when the state changes.

So how do you create an activity?

1. First you need to create a class that inherits activity;

2. In this class you need to implement different callback methods for the life cycle of this activity, where the Oncreat method is the method we have to implement, and we usually initialize the user with some of the necessary parameters of your activity in this method, The most important thing is that we call the Setcontentview method to define the activity's interface.

Start an activity

We can start an activity by calling StartActivity this method and passing a intent parameter, which intent either accurately describes the activity you want to start, Or maybe describe the action you want to take (so the system chooses a more accurate activity for you, and sometimes even another app, depending on the action you describe). A intent can carry data from some of the activtity that are started.

intent   intent  =    new    intent   ( this      signinactivity    class   startactivity   ( intent   
IntentIntent= New Intent(Intent.Action_send);Intent.PutExtra(Intent.Extra_email,Recipientarray);startactivity(Intent);
How do I start an activity that returns results?

Sometimes we want to return a result from the activated activity, and this time we need to call Startactivityforresult to start the activity, and in order to receive the result of the activity returned by this startup, We need to implement the Onactivtyresult method in the first activity. When the second Activiy is finished, it returns a intent result passed to the Onactivtyresult method.

Private voidpickcontact() {    //Create an intent to ' pick ' a contact, as defined by the content provider URI    IntentIntent= New Intent(Intent.Action_pick, Contacts.Content_uri);Startactivityforresult(Intent,pick_contact_request);}@Overrideprotected voidOnactivityresult(intRequestcode, intResultCode, IntentData) {    //If The request went well (OK) and the request is 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 name        Cursorcursor=Getcontentresolver().Query(Data.GetData(),        New String[] {Contacts.Display_name}, NULL, NULL, NULL);        if (cursor.Movetofirst()) { //True If the cursor is not empty            intcolumnindex=cursor.Getcolumnindex(Contacts.Display_name);            Stringname=cursor.getString(columnindex);            //Do something with the selected contact ' s name ...        }    }}
How do I save the status of Activty?

As we mentioned earlier, when an activity stops or pauses, the state of the activity remains, which is right because the object is still in memory when the activity is stopped or paused, This means that all members and states of the activity remain. As a result, the state of any user action is still preserved so that when the activity returns to the foreground, the changes are still there.

However, if the system destroys an activity for memory, the activity object is destroyed, so the system cannot simply recover. Instead, the system will definitely recreate the Activtity object, but the user is unaware of it, so the user may want to restore the acitvity to its original state of information. In this case, you can save the user's important information and state by implementing the Onsaveinstance method in the callback method of the activity's life cycle.

The system passes a bundle object to this method, which holds the key-value pairs with the putstring and Putint methods. When the system kills your application process and the user returns to your activtiy, the activity is recreated and the bundle is passed to the OnCreate and Onrestoreinstancestate methods, but if no state information is saved, This bundle will be a null.

But even if you don't do anything and do not implement the Onsaveinstancestate method, the activity's state will still invoke the Onsaveinstancestate default implementation method. Almost every component in the Android framework will implement this method by default when needed to save the state information of the visible UI, but only those components that have the id attribute defined, the component without the id attribute, and the state system cannot be saved. And only the property values of the components, for other layout-related values are not saved, we must implement the Onsaveinstancestate method to save these values. For example, we can save all the data for a list.

Android Learning Note 8--user interface activty

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.