Android Perfect Solution Bundle implements page jump and retains previous data + Pass value

Source: Internet
Author: User

Preface: Yesterday encountered a problem, I want to achieve page jump, using the bundle, then back to the original page, found that the data is not, and has been error, online search for a lot of information, found to use a startactivityforresult (), however, I also want to in the back of the page to the back of the page data and the previous data passed to the intermediate page data, so it is somewhat complicated, I simply wrote a demo

1. The first is 3 activity===== corresponding to 3 layout ===== all posted out well, a look on the understanding.

Correspondence main3activity

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:app= "Http://schemas.android.com/apk/res-auto"Xmlns:tools= "Http://schemas.android.com/tools"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"Android:background= "@color/coloraccent"android:orientation= "Vertical"Tools:context= "Com.example.kk.test1.Demo.Main3Activity" > <Button Android:id= "@+id/main3_but1"Android:layout_height= "Wrap_content"Android:layout_width= "Wrap_content"Android:text= "First Activity"android:layout_gravity= "Center"/> <TextView Android:id= "@+id/main3_txt1"Android:layout_height= "50DP"Android:layout_width= "100DP"Android:text= "First Activity 1"android:layout_gravity= "Center"Android:background= "@color/colorprimary"/> <TextView Android:id= "@+id/main3_txt2"Android:layout_height= "50DP"Android:layout_width= "100DP"Android:text= "First Activity 2"android:layout_gravity= "Center"Android:background= "@color/colorprimary"/> <TextView Android:id= "@+id/main3_txt3"Android:layout_height= "50DP"Android:layout_width= "100DP"Android:text= "First Activity 3"android:layout_gravity= "Center"Android:background= "@color/colorprimary"/></linearlayout>

Correspondence main4activity

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools=" Http://schemas.android.com/tools "Andro Id:layout_width= "Match_parent" android:layout_height= "match_parent" android:background= "@color/green" Android:ori entation= "vertical" tools:context= "com.example.kk.test1.Demo.Main3Activity" > <button android:id= "@+id/m Ain4_but1 "android:layout_height=" wrap_content "android:layout_width=" Wrap_content "android:text=" 2nd Activity "android:layout_gravity=" center "/> <textview android:id=" @+id/main4_txt1 "Android : layout_height= "50DP" android:layout_width= "100DP" android:text= "2nd activity 1" android:layout_gravity= "cent        Er "android:background=" @color/colorprimary "/> <textview android:id=" @+id/main4_txt2 "     android:layout_height= "50DP"   Android:layout_width= "100DP" android:text= "2nd event 2" android:layout_gravity= "Center" Android:backgro und= "@color/colorprimary"/> <textview android:id= "@+id/main4_txt3" android:layout_height= "5 0DP "android:layout_width=" 100DP "android:text=" 2nd activity 3 "android:layout_gravity=" Center "Androi d:background= "@color/colorprimary"/></linearlayout>

Correspondence main5activity

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools=" Http://schemas.android.com/tools "Andro Id:layout_width= "Match_parent" android:layout_height= "match_parent" android:background= "@color/purple" Android:or ientation= "vertical" tools:context= "com.example.kk.test1.Demo.Main3Activity" > <button android:id= "@+id/ Main5_but1 "android:layout_height=" wrap_content "android:layout_width=" wrap_content "android:text=" 3rd Activities "android:layout_gravity=" center "/> <textview android:id=" @+id/main5_txt1 "Androi d:layout_height= "50DP" android:layout_width= "100DP" android:text= "3rd activity 1" android:layout_gravity= "cen        ter "android:background=" @color/colorprimary "/> <textview android:id=" @+id/main5_txt2 "    android:layout_height= "50DP"    Android:layout_width= "100DP" android:text= "3rd Event 2" android:layout_gravity= "Center" android:backgr ound= "@color/colorprimary"/> <textview android:id= "@+id/main5_txt3" android:layout_height= " 50DP "android:layout_width=" 100DP "android:text=" 3rd activity 3 "android:layout_gravity=" Center "Andro id:background= "@color/colorprimary"/></linearlayout>

2. Then there are 3 activity

=====main3activity

Package Com.example.kk.test1.demo;import Android.content.intent;import android.support.v7.app.AppCompatActivity; Import Android.os.bundle;import android.view.view;import android.widget.button;import Android.widget.TextView;    Import Com.example.kk.test1.r;public class Main3activity extends Appcompatactivity {private Button but1;    Private TextView txt1;    Private TextView txt2;    Private TextView txt3;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity_main3);        Initview ();    Butonclick ();        } private void Initview () {but1= (Button) Findviewbyid (R.ID.MAIN3_BUT1);        txt1= (TextView) Findviewbyid (R.ID.MAIN3_TXT1);        Txt2= (TextView) Findviewbyid (R.ID.MAIN3_TXT2);    txt3= (TextView) Findviewbyid (R.ID.MAIN3_TXT3);            } private void Butonclick () {But1.setonclicklistener (new View.onclicklistener () {@Override public void OnClick (View v) {Intent intent=new Intent (main3activity.this,main4activity.class);                Bundle Bundle=new Bundle ();                String S_txt1=txt1.gettext (). toString ();                String S_txt2=txt1.gettext (). toString ();                String S_txt3=txt1.gettext (). toString ();                Bundle.putstring ("First", S_TXT1);                Bundle.putstring ("Second", S_TXT2);                Bundle.putstring ("Third", S_TXT3);                Intent.putextras (bundle);            StartActivity (Intent);    }        }); }}

=====main4activity

Package Com.example.kk.test1.demo;import Android.content.intent;import android.support.v7.app.AppCompatActivity; Import Android.os.bundle;import android.view.view;import android.widget.button;import Android.widget.TextView;    Import Com.example.kk.test1.r;public class Main4activity extends Appcompatactivity {private Button but1;    Private TextView txt1;    Private TextView txt2;    Private TextView txt3;    Private Intent Intent;    Private bundle bundle;    Private Bundle BUNDLEFROM5;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.LAYOUT.ACTIVITY_MAIN4);        Initview ();        Intent=getintent ();        Bundle=intent.getextras ();        Change_txt ();    Butonclick ();        } private void Initview () {but1= (Button) Findviewbyid (R.ID.MAIN4_BUT1);        txt1= (TextView) Findviewbyid (R.ID.MAIN4_TXT1);        Txt2= (TextView) Findviewbyid (R.ID.MAIN4_TXT2); txt3= (TextView) Findviewbyid (R.ID.MAIN4_TXT3);        } private void Change_txt () {//From the first page fetch something String s_txt1=bundle.getstring        String s_txt2=bundle.getstring ("second");        String s_txt3=bundle.getstring ("third");        Txt1.settext (S_TXT1);        Txt2.settext (S_TXT2);    Txt3.settext (S_TXT3); } private void Butonclick () {//record the data but1.setonclicklistener of the second page before the jump (new View.onclicklistener () {@Ove Rride public void OnClick (View v) {Intent intent=new Intent (main4activity.this,main5activity.cl                );                Bundle Bundle2=new Bundle ();                Bundle2.putstring ("First_1", Txt1.gettext (). toString ());                Bundle2.putstring ("First_2", Txt2.gettext (). toString ());                Bundle2.putstring ("First_3", Txt3.gettext (). toString ());            Intent.putextras (bundle2);//test here before the use of Startactivityforresult (intent,0x717);    }        }); } @Override protected void Onactivityresult (intRequestcode,int resultcode,intent data) {Super.onactivityresult (requestcode,resultcode,data);            if (resultcode==0x717) {Bundlefrom5=data.getextras ();            Txt3.settext (bundlefrom5.getstring ("Main5_txt3"));            Txt1.settext (bundlefrom5.getstring ("first_1"));        Txt2.settext (bundlefrom5.getstring ("first_2")); }    }}

=====main5activity

Package Com.example.kk.test1.demo;import Android.content.intent;import android.support.v7.app.AppCompatActivity; Import Android.os.bundle;import android.view.view;import android.widget.button;import Android.widget.TextView;    Import Com.example.kk.test1.r;public class Main5activity extends Appcompatactivity {private Button but1;    Private TextView txt1;    Private TextView txt2;    Private TextView txt3;    Private Intent Intent;    Private bundle bundle;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.LAYOUT.ACTIVITY_MAIN5);        Initview ();        Addintent ();    Butonclick ();        } private void Initview () {but1= (Button) Findviewbyid (R.ID.MAIN5_BUT1);        txt1= (TextView) Findviewbyid (R.ID.MAIN5_TXT1);        Txt2= (TextView) Findviewbyid (R.ID.MAIN5_TXT2);        txt3= (TextView) Findviewbyid (R.ID.MAIN5_TXT3);        Intent=getintent ();    Bundle=intent.getextras (); } private void Butonclick () {But1.setonclicklistener (new View.onclicklistener () {@Override public vo            ID OnClick (View v) {setresult (0x717,intent);//Go back to finish () by the intention of others.    }        }); } private void Addintent () {//Adds the third part of the content to the second page of bundle bundle.putstring ("Main5_txt3", Txt3.gettext (). toString ())        ;    Intent.putextras (bundle); }}

3. Preview of effects

===== from main3activity===== Click button =====main4activity===== Click Button =====main5activity

===== Last click main5activity button ===== back to Main4activity

It can be found that the second activity saves the value passed from the first activity in the previous event, and also saves the value passed by the third activity.

4. Simply summarize it.

===== of course, the most important thing is a jump function =====startactivityforresult===== it to interact with another activity's setresult, with a unique identifier integer type, casually selected, I used the 0x717

===== personally think that bundles used to transmit data is very convenient, previously thought is the bundle used to transfer data between activities, and then I found that I was too limited

===== really transmits the data is the intention =====intent===== it is the boss, bundle is just a tool to pass the data

===== So, when you need to change the data passed, add bundles first, then don't forget +intent.putextras (bundle)

5. The case is simple, simple copy, you can understand the study.

Good morning, =====2017/7/15.7:56

  

Android Perfect Solution bundle implements page jumps and retains previous data + Pass value

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.