Four components of Android development Learning notes---Activity jump, data transfer (II)

Source: Internet
Author: User

In the previous chapter we review the creation of activity and the life cycle, which we mainly learn about activity jumps and data transfer between activity.

First, activity jumps:

The simple jump between activity is simply a matter of creating two activity and then using startactivity (intent) to jump and see the code:

Intent uio=New  Intent (thisactivityclass,activitybclass); startactivity (UiO);

What is intent?

Android provides the intent mechanism to assist the interaction and communication between applications, intent is responsible for the action of one operation in the application, the action involves data, the additional data description, Android according to this intent description, is responsible for finding the corresponding component, will Intent is passed to the calling component and completes the call to the component. Intent can be used not only between applications, but also between Activity/service within an application. As a result, intent plays a role as a media intermediary, specifically providing information about the components that are called to each other, and decoupling the caller from the callee.

Ii. data transfer between activity:

Below we complete an activity jump pass data and return parameters of example, from activity a click a button, jump to activity b,activity B has two buttons, respectively click two buttons, the interface back to the activity A and use Toast to show different return parameters

1. In activity A, add the data to the bundle in bulk and add the bundle into the intent

        Intent in1=New  Intent (this,activityb.class);        Bundle BUN1=new  bundle ();        Bun1.putstring ("name", "Baihua");        Bun1.putstring ("Age", "" ");        In1.putextras (BUN1);

2.Activity a button click event to do activity jump, and use Startactivityforresult (Intent Intent, int requestcode) method to open a new activity, When a new activity is closed, it returns data to the previous activity

Startactivityforresult (in1,30)

3.Acitivity B return value, we need to get the new activity of the callback data for logic processing, need to rewrite onactivityresult (int requestcode,int resultcode,intent data), Here we define the functional requirements, if the request code is 30, we click on the Activity B button, return to the student number 30 students information, if the request code is 31, we click on the Activity B button, return to the student number 31 students information; Activity B, click button 1, To return the student's name, click Button 2 to return the student's age

First we deal with the data returned in activity B

       Public voidBaihinfo (View v)//the name of the student who corresponds to the study number{Intent in1=NewIntent (); In1.putextra ("Name", "Baih"); In1.putextra ("Age", "24"); Setresult (2, in1);      Using Setresult to return data to activity A, the previous parameter is the return result code finish (); Close the activity when the function is finished processing} Public voidZzzinfo (View v)//age of the corresponding student number{Intent in2=NewIntent (); In2.putextra ("Name", "zzz"); In2.putextra ("Age", "28"); Setresult (1, in2);      Finish (); Close the activity when the function is finished processing}

4. Handle the logic of getting activity B's return result code in activity A, overriding onactivityresult

     Public voidOnactivityresult (intRequestcode,intresultcode,intent data) {                if(requestcode==30)//To determine if the request code in activity a click event is 30, the student who returned to study number 30 {if(resultcode==1)//Judge if the return result code is 1, use. Getextras (). getString () Get the name of 30 students, for others to get the age, here the logic can be more fine, not much write {Bundle er
    =Data.getextras (); String T=er.getstring ("name"); LOG.E ("Baih", T); Toast.maketext (mainactivity. This, T, 2000). Show (); }            Else{Bundle San=Data.getextras (); String y=san.getstring ("Age"); LOG.E ("Baih", y); Toast.maketext (mainactivity. This, Y, 2000). Show ();; }        }        Else{            if(requestcode==31) //To determine if the request code in activity a click event is 31, the student who returned to study number 31 {if(resultcode==1) {Bundle er1=Data.getextras (); String x=er1.getstring ("name"); LOG.E ("Baih", x); Toast.maketext (mainactivity. This, X, 2000). Show (); }                Else{Bundle San1=Data.getextras (); String Z=san1.getstring ("Age"); LOG.E ("Baih", z); Toast.maketext (mainactivity. This, Z, 2000). Show ();; }            }                    }

The variable does not go the rule, in fact this position, mainly wants to introduce two parameters, Requestcode and ResultCode

Requestcode is the request code, using the Startactivityforresult (Intent Intent, int requestcode) method to open the new activity, We need to pass in a request code (the second parameter) for the Startactivityforresult () method. The value of the request code is self-set according to the business need and is used to identify the source of the request. For example: An activity with two buttons, click on both buttons will open the same activity, whether it is the button to open the new activity, when the new activity is closed, the system will call the previous activity Onactivityresult (int requestcode, int resultcode, Intent data) method. In the Onactivityresult () method, if you need to know that the new activity is opened by that button, and you want to make the appropriate business processing

ResultCode is a result code that, in an activity, may use the Startactivityforresult () method to open several different activities to handle different businesses, and when these new activity is closed, The system invokes the previous activity's onactivityresult (int requestcode, int resultcode, Intent data) method. In order to know which new activity the returned data came from

Two very important parameters, which are responsible for controlling the logic control and judgment between the activity data transmission.

Activity transmits data using Intent.putextra (String key,value) for a single parameter pass, or set data parameters into bundles, using Intent.putextra (bundle extras) For centralized transport

Four components of the Android development Learning Note---Activity jump, data Transfer (ii)

Related Article

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.