Getting Started with Android: Jump between activity and callback parameters

Source: Internet
Author: User

Introduced:

        two activity to jump, during the jump, the message is passed from mainactivity to Secondactivity, and when Secondactivity is returned to Mainactivity, Also deliver messages to mainactivity. The first is the mainactivity layout file: Activity_main.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "Horizontal">    <EditTextAndroid:id= "@+id/edit_message"Android:layout_weight= "1"Android:layout_width= "0DP"Android:layout_height= "Wrap_content"Android:text= "Hello world!" />    <ButtonAndroid:id= "@+id/send_message"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Send"Android:onclick= "SendMessage"        />        <!--defining the onclick function -</LinearLayout>
Next is the implementation of Mainactivity:Mainactivity.java
 PackageCom.example.administrator.helloworld;Importandroid.content.Intent;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.util.Log;ImportAndroid.view.View;ImportAndroid.widget.EditText; Public classMainactivityextendsappcompatactivity {//send intent key corresponding to string content     Public  Static  FinalString intent_key= "MESSAGE"; //EditText    PrivateEditText EditText =NULL; Private voidInitview () {EditText=(EditText) Findviewbyid (r.id.edit_message); } @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); //Set LayoutSetcontentview (R.layout.activity_main);    Initview (); }    //Send Message, start secondactivity!     Public voidsendMessage (view view) {Intent Intent=NewIntent ( This, Secondactivity.class); String text=Edittext.gettext (). toString ();        Intent.putextra (Intent_key,text); Startactivityforresult (Intent,0);//the requestcode here should be consistent with the requestcode called in the result handler below    }    //The result handler function, which is called when returned from Secondactivity@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        Super. Onactivityresult (Requestcode, ResultCode, data); if(Requestcode==0 && resultcode==RESULT_OK) {Bundle Bundle=Data.getextras (); String text=NULL; if(bundle!=NULL) Text=bundle.getstring ("Second"); LOG.D ("Text", text);        Edittext.settext (text); }    }}

Note the difference: When the activity jumps, the call becomes Startactivityforresult (intent,0), and the return handler function is implemented. It defines the related operations that are returned.    Note that Requestcode and resultcode need to be compared in return handler functions. followed by the second_activity layout file: Second_activity.xml
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "vertical"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <TextViewAndroid:id= "@+id/textview"Android:layout_width= "Fill_parent"Android:layout_height= "50DP"        />    <ButtonAndroid:id= "@+id/button"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:hint= "click button to return"        /></LinearLayout>

Finally, the implementation of Secondactivity.java:
 PackageCom.example.administrator.helloworld;Importandroid.content.Intent;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportAndroid.view.View.OnClickListener; Public classSecondactivityextendsappcompatactivity {PrivateButton button=NULL; PrivateTextView TextView =NULL; //set the class Buttonlistener implementation interface, Onclicklistener, where you can specify a different ID for the button corresponding to the different click events//This can be used to extract code to improve the readability of the Code    Private classButtonlistenerImplementsOnclicklistener {@Override Public voidOnClick (View v) {Switch(V.getid ()) { Caser.id.button:intent Intent=getintent (); //this uses bundles to transmit data .Bundle bundle =NewBundle (); //The transferred content is still in the form of a key-value pairBundle.putstring ("Second", "Hello World from secondactivity!");//postback message, Hello World from secondactivity!Intent.putextras (bundle);                    Setresult (result_ok,intent);                    Finish ();  Break; }        }    }    //Initialize View     Public voidInitview () {button=(Button) Findviewbyid (R.id.button); TextView=(TextView) Findviewbyid (R.id.textview); Button.setonclicklistener (NewButtonlistener ()); Intent Intent=getintent (); String text=Intent.getstringextra (Mainactivity.intent_key);    Textview.settext (text); } @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (r.layout.second_activity);    Initview (); }}

First of all, attention to the button set listener event processing, through the class Buttonlistener implementation of the interface Onclicklistener, in which different button IDs can be differentiated by switch to achieve different actions for different buttons. Then note that the bundle object is used to pass messages between the activity.    Before ending the activity, call Setresult (result_ok,intent) and send the message that needs to be returned to the first activity. :

Getting Started with Android: Jump between activity and callback 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.