Android Learning Note (iv)--Explore intent again

Source: Internet
Author: User

  This series is the "first line of Android Code" Learning notes, if there are errors and omissions, please correct me!

We can use Intent to start an activity, and we can pass the data at the start of the activity, and take a look at the following:

One, the next activity to pass data
The Intent provides a series of overloads of the PutExtra () method that can temporarily present the data we want to pass in Intent, and once another activity is initiated, it is only possible to remove the data from the Intent. For example, we can create a string in mainactivity and then pass it to the second_activity:

1) First build a good intent in mainactivity:

@Override  Public void OnClick (View v) {    = "Hello second_activity";     New Intent (mainactivity.  this, second_activity. class );    Intent.putextra ("Extra_data", data);    StartActivity (intent);}

We use an explicit intent approach to start second_activity and pass a string through the PutExtra () method. Note here that the PutExtra () method receives two parameters, the first parameter is the key name, which is used to fetch the value from the intent, and the second parameter is the data that is actually being passed.

2) Extract the intent data from the second_activity:

1  Public classSecond_activityextendsActivity {2 3 @Override4     protected voidonCreate (Bundle savedinstancestate) {5         Super. OnCreate (savedinstancestate);6 Setcontentview (r.layout.secend);7Intent Intent =getintent ();8String data = Intent.getstringextra ("Extra_data");9LOG.D ("Secondactivity", data);Ten     } One  A}

Here we get through the Getintent () method to start the Second_activity Intent, and then call the Getstringextra () method, passing in the corresponding key value, you can get the data passed. This is because we are passing strings, so we use the Getstringextra () method to get the data passed, and if we pass the integer data, the Getintextra () method is used, the Boolean data is passed, and the Getbooleanextra () method is used. And so on

Start the program at this point, and run the following effect:

  

When you press button 1, you can see that the incoming string is printed in Logcat:

  

Ii. returning data to the previous activity
One of the Startactivityforresult () methods in activity is also used to initiate activity, which returns a result to the previous activity when the activity is destroyed. We can use it here:

  1) Modify the mainactivity in the onclick () Method:

1  Button1.setonclicklistener (new   Onclicklistener () { 2   @Override  Span style= "color: #008080;" >3  public  void   OnClick (View v) { 4  Intent Intent = new Intent (Mainactivity. This , second_activity. class   5  Startactivityforresult (Intent, 1 6   7  }); 

Here we use the Startactivityforresult () method to start the second_activity, the request code as long as a unique value can be, here 1 can be passed.

2) Register a button 2 in second_activity and add the logic to return the data:

1  Public classSecond_activityextendsActivity {2 @Override3     protected voidonCreate (Bundle savedinstancestate) {4         Super. OnCreate (savedinstancestate);5 Setcontentview (r.layout.second);6 7Button button2 =(Button) Findviewbyid (r.id.button_2);8Button2.setonclicklistener (NewOnclicklistener () {9 @OverrideTen              Public voidOnClick (View v) { OneIntent Intent =NewIntent (); AIntent.putextra ("Data_return", "Hello mainactivity"); - Setresult (RESULT_OK, intent); - finish (); the             } -         }); -     } -}

We've built a Intent, but this Intent is just for passing data, and it doesn't specify any "intent." The data to be passed is then stored in Intent and the Setresult () method is called. This method is specifically used to return data to the previous activity. The Setresult () method receives two parameters, the first parameter is used to return the processing result to the previous activity, typically only the RESULT_OK or result_canceled values are used, the second parameter is to pass the Intent with the data back, and then call
The finish () method to destroy the current activity.

3) override the Onactivityresult () method of the main activity:
Since we are using the Startactivityforresult () method to start Second_activity, the Onactivityresult () method of the previous activity is recalled after Second_activity is destroyed, So we need to rewrite this method in Mainactivity to get the returned data:

1 @Override2 protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {3     Switch(requestcode) {4      Case1:5             if(ResultCode = =RESULT_OK) {6String returneddata = Data.getstringextra ("Data_return");7LOG.D ("Mainactivity", returneddata);8             }9              Break;Ten     default: One     } A}

The Onactivityresult () method has three parameters, the first parameter Requestcode, which is the request code that we passed in when the activity was started. The second parameter, ResultCode, is the result of the processing we passed in when we returned the data. The third parameter, data, is the Intent that carries the returned data.

Because a primary activity might invoke the Startactivityforresult () method to initiate many different activities, the data returned by each activity is recalled to the Onactivityresult () method. So we need to judge Requestcode (request code) to determine the source of the activity, and to judge the execution of the activity by ResultCode.

Third, the data is returned when the back key is pressed

1 @Override 2      Public void onbackpressed () {3     New Intent (); 4     Intent.putextra ("Data_return", "Hello firstactivity"); 5     Setresult (RESULT_OK, intent); 6     Finish (); 7 }

End.

Android Learning Note (iv)--Explore intent again

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.