Use intent in Android to achieve activity with data jumps

Source: Internet
Author: User

As we all know, startactivity () is used to switch the jump activity, if you want to book data in another activity, you only need to use the Intent.putextra () method in the source activity to get out of the data, It's okay to use the Intent.get**extra () method to receive data in the corresponding activity.

So Startactivityforresult () method, it is also used to take data to jump activity, but these two methods specific what difference?

This is going to say that with the direction of the data jump ,

The first method can only send the data past, but if the child activity needs to transfer back some data?

You might say, in child activity, using the Intent.putextra () method to write the data, write the Get* method in the parent activity is OK,

But, have you ever wondered where the Get method in the parent activity is going to be written?

I think, of course, you would say you want to get the data when the activity starts, so of course it's written in the OnCreate method,

So the question is, if you write the Get* method in the OnCreate method in the parent activity to read (get) the data,

Then it means that when you start the parent activity you execute the Get* method (because the first way to do this is OnCreate), and the parent activity is the main method, it must be the activity that the program first executes,

In other words, there is no child activity at this time, there is no data passed through the intent, so when the execution of OnCreate, execution to the Get* method will not be read when the data error throws an exception,

So how do you get the parent activity to retrieve the data from the child activity?

This is the second method: the Startactivityforesult () method works.

Here's how this method is implemented to read the data returned by the activity.

First look at the parameters of this method:startactibityforresult (Intent intent,int requestcode)

Find the first parameter is the intent type, this does not have to say, look at the second int type, see the meaning is "request Code", in fact, the function is the request code, this specific in the following said,

As long as you use Startactivityforesult (), then it is inevitable to add a onactivityresult (int requestcode, int resultcode, Intent Intent in this class) ) This method,

You can call it: callback method (the child activity callback method of the parent activity)

This method, like OnCreate (), is the member method of this class , which means that this method is not in the OnCreate method (This avoids the get* method that initiates the activity),

What is the use of this method, you can understand that it is used to monitor the child activity of the method, as long as the child activity at the end (call the Finish () method), it will be executed, so that the implementation of the "callback" function

Here's a look at the parameters of this method:

int Requestcode: corresponds to the int requestcode parameter in the Startactibityforresult (Intent intent,int Requestcode) method, which can be understood as Request Code

int ResultCode: see name, result code , this is set in child activity

Intent Intent: This doesn't have to be said.

Then we'll look at the specific effects of these parameters,

1) int requestcode, the request code, and the int in the startactibityforresult (Intent intent,int Requestcode) method in the parent activity Requestcode parameter Correspondence ,

It is used to determine which component in the parent activity requested the child activity, because there may be multiple buttons or other components in the parent activity that can issue requests to enter new child activity,

The Onactivityresult () method is only one in the parent activity, so it can be marked clearly (required).

2) int ResultCode, result code, it is also the role of the tag, it and the child activity using setresult (int resultcode,intent Intent) method to set the resultcode parameter corresponding to the

Then its role is related to the child activity, it can be used to determine which sub-activity at the end of the callback method back and forth, because the parent activity may need to jump to a number of different sub-activity

The Onactivityresult () method is only one in the parent activity, so it can be marked clearly (required).

3) Intent Intent, intent of the request execution callback method

Generally speaking, the following gives a Java code to visually see

//Parent Activity Core code:Button button1=(Button) Findviewbyid (R.id.button1); Button Button2=(Button) Findviewbyid (R.id.button2); //oncreate Core code:onCreate () {//set the Listener for button 1, put data 100 named Data1, set the request to 1Button1.setonclicklistener (NewView.onclicklistener () {OnClick () {Intent intent1=NewIntent (); Intent.setclass ( This, one.class); Intent.putextra ("Data1", "100"); Startactivityforresult (Intent,1); }        }        //set the Listener for button 2, put data 200 named Data2, set the request to 2Button2.setonclicklistener (NewView.onclicklistener () {OnClick () {Intent Intent2=NewIntent (); Intent.setclass ( This, both.class); Intent.putextra ("Data2", "200"); Startactivityforresult (Intent,2); }        }     }        //callback MethodOnactivityresult (intRequestcode,intResultCode, Intent Intent) {        Super. Onactivityresult (Requestcode, ResultCode, intent); Switch(Requestcode) {//determine which button in the parent activity             Case1://if it's a button 1                            }              Case2://if it's a button 2                            }        }        Switch(ResultCode) {//determine which child activity is             Case1://if it's a sub-activity1                            }              Case2://if it's a sub-activity2                            }        }//******************************************************************
//Sub-Activity1:one.java core codeButton button1=(Button) Findviewbyid (R.id.button1); OnCreate () {//set the Listener for button 1, set the result to 1Button1.setonclicklistener (NewView.onclicklistener () {OnClick () {
Setresult (1, intent); Finish (); } }

//*********************************************************************//Sub-Activity2:two.java core codeButton button1=(Button) Findviewbyid (R.id.button1); OnCreate () {//set the Listener for button 1, set the result to 2Button1.setonclicklistener (NewView.onclicklistener () {OnClick () {Setresult (2, intent); Finish (); } }

So tired.

The code is written directly, and some trivial issues are ignored.

After that, write the code in Eclipse and stick to it.

That's all for today.

Use intent in Android to achieve activity with data jumps

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.