Android Summary series: A few key functions in activity

Source: Internet
Author: User

Focus on the development of the Android field. Look up at the starry sky and be grounded. --Better memory than bad blog Android Summary series: A few key functions in activity

Activity, one of the four basic components of Android, contains a large number of functions that interact with other components, intent, widgets, and system services. In this paper, the main selection of practical projects in the development of common, but fully understand the need to have a certain in-depth understanding of several functions to explain, follow-up this article will be updated as necessary.

1. Startactivityforresult/onactivityresult/setresult function Combination

Referring to this kind of function combination, I believe that as long as the Android development is familiar, this function combination is mainly used in the following scenario: The user clicks a button on activity A, jumps to B activity, then the user in B activity to do some specific actions, Return to a activity when the operation is complete, and often return some of the data that is operating in activity B to activity A.

As in the scene above, A-B needs to be opened by means of Startactivityforresult (). The following are the specific methods:

1 Button.setonclicklistener (new View.onclicklistener () {2     @Override3 public     void OnClick (View v) {4         Intent Intent = new Intent (aactivity.this, Bactivity.class); 5         Startactivityforresult (Intent, 1); 6     }7});

Where the Startactivityforresult first parameter is intent, it can be passed directly through intent for additional arguments that need to be passed. Where bundles are optional parameters. The second parameter is Requestcode, a business request code.

B activity, after processing or the corresponding user operation, the end of itself, you need to pass the data back to a setresult.

1 Btnclose.setonclicklistener (new View.onclicklistener () {2 public     void OnClick (View v) {3          4         // Data to be returned is deposited into Intent 5         Intent Intent = new Intent (); 6         Intent.putextra ("name", "Corn"); 7          8         //Set return Data 9         Setresult (RESULT_OK, intent);         //close Activity12         finish ();     }14});

Next, a takes over the data of B callbacks.

1 @Override 2 protected void Onactivityresult (int requestcode, int resultcode, Intent Intent) {3     String name;  4     //Get data for B backhaul 5     if (ResultCode = = RESULT_OK) {6          name = Intent.getstringextra ("name"); 7     } else if (RESULTC Ode = = result_canceled) {8         //... 9     }10}

In this combination of functions, you need to be aware of the following issues:

1. According to the actual needs of the project, it is particularly important to note that the Requestcode must be >= 0, otherwise the effect will become startactivity () effect;

2.resultCode represents the resulting state after processing in B, and the system internally defines three states of RESULT_OK, result_canceled, and Result_first_user. Of course, you can define it as any int type identity state.

3. Sometimes in complex business logic, a startactivityforresult to B may exist, while C also startactivityforresult to B, and Requestcode may be the same (to signify consent to a business request), It may be necessary to determine the source of this request (from a or c) in B. At this point, you can pass the intent parameter. I believe that we are familiar with, in fact, the activity class also provides the corresponding function can get to the source activity type function: Getcallingactivity (). Note, however, that this function is valid only for Startactivityforresult, and the returned result contains the completion package name.

callback function call timing in 4.A it is important to note that its invocation occurs after the onpause of B, before the onrestart of a (if B finishes obscuring a), and must precede onresume.

5. This function combination for the start mode of B is singletask or singinstance will be invalidated. At this point, Onactivityresult will directly callback after the onpause of a, and ResultCode is result_canceled.

2.Movetasktoback

As mentioned in the article "Android Summary series: Activity life cycle", simulation of the current mainstream application is not forced to quit the application or directly end the root activity, but take the class home key effect, this function can be directly implemented, very practical.

1 @Override2 public void onbackpressed () {3     Movetasktoback (TRUE); 4}

This method moves the task where the current activity resides directly to the background while preserving the activity order and state.

3.onNewIntent Call Timing

Onnewintent can only be recalled in the following scenario: the activity that is currently started by intent is not a re-complete new instance, but rather a reuse of an existing instance (such as the Singletop startup mode is set, or Flag_ Activity_single_top Intent Flags or set Intent.flag_activity_clear_top | Intent.flag_activity_single_top, and so on, is set to Singletask or single. This will be done to onnewintent). After the onnewintent call, the callback will continue to onrestart,onresume ...

4.onsaveinstancestate/onrestoreinstancestate Call Timing

Onsaveinstancestate Call Timing: When activity becomes "easy" to be destroyed by the system, Onsaveinstancestate is called back, unless the activity is actively destroyed by the user, such as when the user presses the back key.

Notice the double quotes above, what is "easy"? The implication is that the activity has not been destroyed, but merely a possibility. What are the possibilities?

1. When the user presses the home button;
2. Press and hold the home key to select another program to run;
3. Press the power button (the screen display is turned off);
4. When a new activity is initiated from activity A;
5. When the screen orientation is toggled, for example, when switching from the vertical screen to the horizontal screen.

When the onrestoreinstancestate is called, activity a "does" is destroyed by the system, and if it is only stuck in the case where there is such a possibility, then the method will not be called. In addition, the bundle parameters of the Onrestoreinstancestate are passed to the OnCreate method, or you can choose to do a data restore in the OnCreate method.

1 @Override 2     protected void Onrestoreinstancestate (Bundle savedinstancestate) {3         //TODO auto-generated method Stub 4         super.onrestoreinstancestate (savedinstancestate); 5         savedinstancestate.getstring ("name", ""); 6          7     } 8  9     @Override10 public     void Onsaveinstancestate (Bundle savedinstancestate) {         One Super.onsaveinstancestate (savedinstancestate);         savedinstancestate.putstring ("name", "Corn");     

Or in the OnCreate:

1 public class Aactivity extends Actionbaractivity {2  3     private String name; 4  5     @Override 6     Protect Ed void OnCreate (Bundle savedinstancestate) {7         super.oncreate (savedinstancestate); 8         Setcontentview ( R.LAYOUT.A); 9         if (savedinstancestate! = null) {One             name = savedinstancestate.getstring ("name");         }13     }15 16}

It is important to note that when Onsaveinstancestate is called, its invocation takes place in the activity life cycle. Take A->b as an example, the Onsaveinstancestate call in A takes place in B:onresume, a:onpause, B:oncreate, A:onsaveinstancestate- > A:onstop.

Onsaveinstancestate is often used to store important state data in the current activity in the application to prevent the activity from accidentally being killed by the system and cannot find the previous state when the user returns again. As with using multiple fragment to implement menu functions in an activity, it is a good idea to record the fragment ID for the current menu in this function.

Transfer from http://www.cnblogs.com/lwbqqyumidi/p/3776184.html

Android Summary series: A few key functions in activity

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.