Android Intent implement page jumps between pages

Source: Internet
Author: User

What is intent
Intent can be understood as messengers (intentions)
Intent to help complete the communication between Android components
Intent to achieve a gradual jump in the page
1.startActivity (INETNT)
2.startActivityForResult (Intent, Requestcode);
Onacitivtyresult (int requestcode, int resultcode, Intent data)
Setresult (ResultCode, data);

Create two XML files Firstactivity.xml and Secondactivity.xml first.

<?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= "vertical" >        <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "A jump between pages via intent \ n" to implement a page jump with no return result via button 1 \ n A page that has a return result via button 2 " />    <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Button 1" />    <ButtonAndroid:id= "@+id/button2"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "button 2" />    <TextViewAndroid:id= "@+id/textview1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "The results after the page jump will be displayed here" /></LinearLayout>
Firstactivity.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= "vertical" >        <TextViewAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "Click to return to first activity" />    <ButtonAndroid:id= "@+id/button1"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:text= "button" />    </LinearLayout>
Secondactivity.xml

Register the two XML in Androidmanifest.xml and set Firstactivity.xml to be the XML that is loaded when it is started.

        <ActivityAndroid:name= "Com.example.intentdemo.FirstActivity"Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>        </Activity>        <ActivityAndroid:name= "Com.example.intentdemo.SecondActivity"Android:label= "@string/app_name" >        </Activity>

The Firstactivity.java Buttton1 is used to implement jumps through the StartActivity () method.

        Button1.setonclicklistener (new  Onclicklistener () {                        @Override            publicvoid  OnClick (view view) {                new Intent (firstactivity.  this, secondactivity. class );                StartActivity (intent);            }        });

The Firstactivity.java Buttton2 is used to implement jumps through the Startactivitywithresult () method.

        Button2.setonclicklistener (new  Onclicklistener () {            @Override            publicvoid  OnClick (View arg0) {                new Intent (firstactivity.  this, secondactivity. class );                 int requestcode = 1;    // take it yourself.                 Startactivityforresult (Intent, Requestcode);            }                    );

The second activity returns the result and destroys itself through finish ().

        Button.setonclicklistener (new  Onclicklistener () {                        @Override            publicvoid  OnClick (view view) {                int resultcode = 2;                 New Intent ();                Data.putextra ("Data", "This is the result of a second activity return");                Setresult (ResultCode, data);                Finish ();            }        });    }

The first activity obtains the returned result through the Onactivityresult () method and displays it in TextView.

    @Override    protectedvoid onactivityresult (intint  ResultCode, Intent data) {        super. Onactivityresult (Requestcode, ResultCode, data);         if (Requestcode = = 1 && ResultCode = = 2) {            textview.settext (Data.getstringextra ("data"));        }    }
 PackageCom.example.intentdemo;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button;ImportAndroid.widget.TextView; Public classFirstactivityextendsActivity {PrivateButton button1; PrivateButton button2; PrivateTextView TextView; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (r.layout.firstactivity); TextView=(TextView) Findviewbyid (R.ID.TEXTVIEW1); Button1=(Button) Findviewbyid (R.id.button1); Button1.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (view view) {Intent Intent=NewIntent (firstactivity. This, Secondactivity.class);            StartActivity (Intent);                }        }); Button2=(Button) Findviewbyid (R.id.button2); Button2.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {Intent Intent=NewIntent (firstactivity. This, Secondactivity.class); intRequestcode = 1;//take it yourself.Startactivityforresult (Intent, Requestcode);    }                    }); } @Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent data) {        Super. Onactivityresult (Requestcode, ResultCode, data); if(Requestcode = = 1 && resultcode = 2) {Textview.settext (Data.getstringextra ("Data")); }    }}
Firstactivity.java
 PackageCom.example.intentdemo;Importandroid.app.Activity;Importandroid.content.Intent;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.widget.Button; Public classSecondactivityextendsActivity {Privatebutton button; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);                Setcontentview (r.layout.secondactivity); Button=(Button) Findviewbyid (R.id.button1); Button.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (view view) {intResultCode = 2; Intent Data=NewIntent (); Data.putextra ("Data", "This is the result of a second activity return");                Setresult (ResultCode, data);            Finish ();    }        }); }    }
Secondactivity.java

Effect:


Android Intent implement page jumps between pages

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.