Android Learning notes (v)

Source: Internet
Author: User

Intent is not only used to start an activity, intent can also pass parameters when initiating an activity.

1. Passing data to the next activity

When the activity is started to pass data, intent provides a series of overloads of the Putextra () method that can temporarily present the data to be passed in intent, starting another activity,

You just need to get this data out of the intent again.

For example, there is a string in firstactivity that now passes the string to secondactivity, with the following code:

Button1.setonclicklistener (new  Onclicklistener () {                @Override    publicvoid  OnClick (View v) {    = "Hello secondactivity";     New Intent (firstactivity.  this, secondactivity. class );    Intent.putextra ("Extra_data", data);    StartActivity (intent);                            }        });

The above code starts secondactivity with the display intent method, and then passes a string through the Putextra () method.

The PutExtra () method receives two parameters, the first parameter is the key, which is used to fetch the value from the intent, and the second parameter is the data that is actually being passed.

The parameters passed in the secondactivity are then taken out and printed out with the following code:

    protected void onCreate (Bundle savedinstancestate) {        super. OnCreate (savedinstancestate);        Requestwindowfeature (window.feature_no_title);        Setcontentview (r.layout.second_layout);             = getintent ();             = Intent.getstringextra ("Extra_data");            LOG.D ("secondactivity", data);}

In the above code, through the Getintent () method to get to start the secondactivity intent, through the Getstringextra () method, passing in the corresponding key value, you can get the data passed.

Note: Pass strings with Getstringextra (); Integer data with Getintextra (); Pass Boolean data with Getbooleanextra ().

2. Return data to the previous activity

There is a Startactivityforresult () method in activity that can also be used to initiate activities,

The Startactivityforresult (Intent Intent, int requestcode) method receives two parameters:
* The first parameter is still a intent;
* The second parameter is the request code, which is used to determine the source of the data in subsequent callbacks.

Modify the event for the button in the Firstactivity code as follows:

Button1.setonclicklistener (new  Onclicklistener () {                @Override    publicvoid  OnClick (View v) {    new Intent (firstactivity.  this, secondactivity. class );     // The request code is available as long as it is a unique value.     Startactivityforresult (Intent, 1);                            }        );

Here, use the Startactivityforresult () method to start the secondactivity, as long as the request code is a unique value.
Then modify the button's Click event in the Secondactivity and add the logical code that returns the data, with the following code:

Button2.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View v) {Intent Intent=NewIntent (); Intent.putextra ("Data_return", "Hello firstactivity"); //Call the Setresult () method, which is specifically used to return data to the previous activity. //the Setresult () method receives the first song parameter, and the first parameter is used to return the processing result to the previous activity, typically using RESULT_OK or result_canceled//The second parameter is to pass the intent with the data back, and then call the finish () method to destroy the current activity. Setresult (RESULT_OK, intent);                            Finish (); }        });

In the code above, we're going to create a intent, which is only used to pass data, and it doesn't specify any intent. Store the data to be passed in intent, and then call the Setresult () method

Receives two parameters.

Call the Setresult () method, which is specifically used to return data to the previous activity. The Setresult () method receives two parameters,

* The first parameter is used to return processing results to the previous activity, typically using RESULT_OK or result_canceled
* The second parameter is to pass the intent with the data back and then call the finish () method to destroy the current activity.

Because the Startactivityforresult () method is used to start the secondactivity activity, the Onactivityresult () method of the previous activity is recalled after Secondactivity is destroyed. So you need to rewrite the Onactivityresult () method in firstactivity to get the return data. The Onactivityresult (int requestcode, int resultcode, Intent data) method has three parameters:
* The first parameter: Requestcode, is the request code that is passed in when the activity is started;
* Second parameter: ResultCode, the result of the incoming processing when the data is returned.
* The third parameter: data, with the intent of the return.
It is possible to invoke the Startactivityforresult () method in an activity to initiate a different activity, and the data returned by each activity will be recalled to the Onactivityresult () method, So first check the value of the Requestcode to determine the source of the data, and then in the value of ResultCode to determine whether the processing results are successful, and finally from the intent to fetch the returned data.

If the firstactivity is returned by the back key instead of by a button in secondactivity, how can I return the data?
Here, if you return data through the back key, you can return data by overriding the Onpressed () method in Secondactivity, with the following code:

// if it is not returned by a button in Secondactivity, but is returned by the button back key, this requires overriding the Onbackpressed () method to return the data     Public void onbackpressed () {        new  Intent ();        Intent.putextra ("Data_return", "Hello firstactivity");        Setresult (RESULT_OK, intent);        Finish ();    }

This way, when the user presses the back key, the program executes the onpressed () method.

Android Learning notes (v)

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.