Android Development Series (13): Intent implement transitions between activity and Startactivityforresult pass parameters

Source: Internet
Author: User

One, the intent realizes the switch between the activity 1, the constructor method:


2, SetClass Method:

Intent Intent = new Intent (); Intent.setclass (this, otheractivity.class); Set the component to be activated startactivity (intent);  

3, Setclassname Method:

Intent Intent = new Intent () Intent.setclassname (This, "cn.itcast.activitys.OtherActivity"); StartActivity (Intent);

4, SetComponent Method:

Intent Intent = new Intent () intent.setcomponent (new ComponentName (This,otheractivity.class)); StartActivity (Intent) ;

Second, Startactivityforresult used to transfer parameters

First, we defined two activity, which is two interfaces: A and B.

A interface contains a button: Login

The B interface contains two input boxes and a login button: User name and password input box, login button.

Requires that a string be passed from the A interface, and the B interface can be displayed.

b interface input user name and password, after clicking on the login, you can display in a interface.


First, let's look at the interface of a interface:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" fill_parent "    android:layout_height=" fill_parent "    android:o rientation= "vertical" >        <button         android:layout_width= "fill_parent"        android:layout_height= " Wrap_content "        android:text=" @string/button "        android:onclick=" openactivity "        />    </ Linearlayout>
The top contains a button that defines an OnClick property, setting the click Method: Openactivity


We then write the Java call code for the A Interface (Mainactivity.java):

Package Cn.itcast.activitys;import Android.app.activity;import Android.content.componentname;import Android.content.intent;import Android.os.bundle;import Android.view.view;import Android.widget.Toast;public class Mainactivity extends activity {/** Called when the activity is first created. */@Override public void OnCreate (        Bundle savedinstancestate) {super.oncreate (savedinstancestate);    Setcontentview (R.layout.main);        } public void Openactivity (View v) {//in Intent Intent = new Intent (this, otheractivity.class);    Bundle bundle = new bundle ();    Bundle.putstring ("Result", "I am the parameter passed over from Mainactivity");        Intent.putextras (bundle); Startactivityforresult (Intent, 200); Two parameters: The first one is the intent object, the second is the request code Requestcode} @Overrideprotected void Onactivityresult (int requestcode, int resultcode, Intent D ATA) {String name= "", password= "", if (ResultCode = = 30) {//Determine if the return code is 30name = Data.getstringextra ("name"). ToString (); Password = Data.getstringextra ("password").ToString (); Toast.maketext (This, "Your login username is:" +name+ ", the password is:" +password, 1). Show ();}    Super.onactivityresult (Requestcode, ResultCode, data);} }
In openactivity (View V) This method, we define a bundle object, then pass in a property parameter named "Result" and place it in the intent object, through Startactivityforresult ( Intent Intent,int Requestcode) This method is passed. We can see that we've passed this file to Otheractivity.java .


Then, we have to look at the code inside the Otheractivity.java:

Package Cn.itcast.activitys;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.view.view;import android.widget.edittext;import Android.widget.textview;import android.widget.Toast ;p ublic class Otheractivity extends Activity {private EditText name;private EditText password; @Overrideprotected void OnC Reate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.otheractivity); I Ntent intent = getintent (); The object used to activate it: The intent obtained here is the last activity passed intentbundle bundle = Intent.getextras (); String result = bundle.getstring ("result"); Toast.maketext (This, result, 1). Show ();} public void closeactivity (View v) {Intent data = new Intent (); name = (EditText) This.findviewbyid (r.id.name);p Assword = (Ed Ittext) This.findviewbyid (R.id.password);d Ata.putextra ("name", Name.gettext (). toString ());d Ata.putextra (" Password ", Password.gettext (). toString ()); Setresult (data); Set the return Data This.finish (); Close Current Activity}}
In the Oncreae () method, we call the Toast object to display the obtained parameters in the interface.

We then call the OnClick method of the button in the Otheractivity.xml interface to invoke the Closeactivity () method.

With this method, we can pass the username and password entered in the text box through the setresult (int resultcode,intent Intent) method back to the A interface, and call This.finish () to close the current interface.


Next, we look at the source of the B interface: Otheractivity.xml

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical "&G    T <textview android:layout_width= "fill_parent" android:layout_height= "Wrap_content" android:text= "@string/name"/> <edittext android:layout_width= "fill_parent" android:layout_height= "Wra         P_content "android:id=" @+id/name "/> <textview android:layout_width=" Fill_parent " android:layout_height= "Wrap_content" android:text= "@string/password"/> <edittext androi    D:layout_width= "Fill_parent" android:layout_height= "wrap_content" android:id= "@+id/password"/> <button android:layout_width= "fill_parent" android:layout_height= "wrap_content" android:text= "@ String/closebutton "android:onclick= "Closeactivity"/></linearlayout> 


When the This.finish () method is called, the B interface ends and the a interface is returned.

As we can see, there is an overriding method in Mainactivity.java: Onactivityresult (). This method is provided by the system and can be overridden in "right-click->source->override/implement Methods".

This method is called after Setresult () is returned, in which we determine if the return code is passed in the Otheractivity.java 30, and if so, the user name and password are displayed with the Toast object.





Android Development Series (13): Intent implement transitions between activity and Startactivityforresult pass parameters

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.