Data exchange between Android two activity

Source: Internet
Author: User
<span id="Label3"></p><p><p>1. without data</p></p><pre><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); Next</span></span>=<span style="color: #000000;"><span style="color: #000000;">(Button) Findviewbyid (r.id.next); Next.setonclicklistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Button.onclicklistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View arg0) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <strong><strong>Intent intent=<span style="color: #0000ff;">new</span> <span style="color: #000000;"> Intent (); Intent.setclass (mainactivity.</span> <span style="color: #0000ff;"></span>this, Second. <span style="color: #0000ff;">class</span> </strong></strong><span style="color: #000000;"><span style="color: #000000;"><strong>); StartActivity (intent);</strong> } }); }</span></span></pre><p><p>2. Data from the first activity is passed to the second activity</p></p><p><p>Send Side</p></p><pre> <span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); Next</span></span>=<span style="color: #000000;"><span style="color: #000000;">(Button) Findviewbyid (r.id.next); Content</span></span>=<span style="color: #000000;"><span style="color: #000000;">(EditText) Findviewbyid (r.id.content); Next.setonclicklistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Button.onclicklistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View arg0) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span>String name=<span style="color: #000000;"><span style="color: #000000;">content.gettext (). toString (); <strong>Intent Intent</strong> </span></span><strong><strong>=<span style="color: #0000ff;">new</span> <span style="color: #000000;"> Intent (); Intent.putextra (</span>"name"<span style="color: #000000;">, name); </span> <span style="color: #008000;">//</span><span style="color: #008000;">toast.maketext (mainactivity.this, name, toast.length_short). show ();</span> Intent.setclass (mainactivity. <span style="color: #0000ff;"> </span>this, Second. <span style="color: #0000ff;">class</span> </strong></strong><span style="color: #000000;"><span style="color: #000000;"><strong>); StartActivity (intent); </strong> } }); }</span></span></pre><p><p>Receiving end</p></p><pre><span style="color: #000000;"><span style="color: #000000;">@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_second); Name</span></span>=<span style="color: #000000;"><span style="color: #000000;">(TextView) Findviewbyid (r.id.name); </span>age</span>=<span style="color: #000000;"><span style="color: #000000;">(EditText) Findviewbyid (r.id.age); </span>back</span>=<span style="color: #000000;"><span style="color: #000000;">(Button) Findviewbyid (r.id.back); <strong>Intent Intent</strong> </span></span><strong><strong>=<span style="color: #000000;">getintent (); String na</span>=intent.getstringextra ("name"<span style="color: #000000;">); </span> <span style="color: #008000;">//</span><span style="color: #008000;">toast.maketext (second.this, na, toast.length_short). show ();</span> </strong></strong><span style="color: #000000;"><span style="color: #000000;"> <strong>name.settext (na);</strong> }</span></span></pre><p><p>3. A second activity transmits data to the first activity</p></p><p><p>First activity</p></p><pre> <span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); Next</span></span>=<span style="color: #000000;"><span style="color: #000000;">(Button) Findviewbyid (r.id.next); Content</span></span>=<span style="color: #000000;"><span style="color: #000000;">(EditText) Findviewbyid (r.id.content); Next.setonclicklistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Button.onclicklistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View arg0) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span>String name=<span style="color: #000000;"><span style="color: #000000;">content.gettext (). toString (); <strong>Intent Intent</strong> </span></span><strong><strong>=<span style="color: #0000ff;">new</span> <span style="color: #000000;"> Intent (); Intent.putextra (</span>"name"<span style="color: #000000;">, name); </span> <span style="color: #008000;">//</span><span style="color: #008000;">toast.maketext (mainactivity.this, name, toast.length_short). show ();</span> Intent.setclass (mainactivity. <span style="color: #0000ff;"> </span>this, Second. <span style="color: #0000ff;">class</span> </strong></strong><span style="color: #000000;"><span style="color: #000000;"><strong>);</strong> <strong>Startactivityforresult (intent, result_code); </strong> } }); } @Override </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span>Onactivityresult (<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>requestcode,<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span><span style="color: #000000;"><span style="color: #000000;">resultcode, Intent Data) { </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. Onactivityresult (requestcode, resultcode, data); </span></span><strong><strong> <span style="color: #0000ff;">if</span>(requestcode==<span style="color: #000000;">result_code) { </span> <span style="color: #0000ff;">if</span>(resultcode==<span style="color: #000000;">Second.result_code) { Bundle Bundle</span>=<span style="color: #000000;">Data.getextras (); String con</span>=bundle.getstring ("age"</strong></strong><span style="color: #000000;"><span style="color: #000000;"><strong>); Content.settext (con);</strong> } } }</span></span></pre><p><p>A second activity</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_second); Name</span></span>=<span style="color: #000000;"><span style="color: #000000;">(TextView) Findviewbyid (r.id.name); </span>age</span>=<span style="color: #000000;"><span style="color: #000000;">(EditText) Findviewbyid (r.id.age); </span>back</span>=<span style="color: #000000;"><span style="color: #000000;">(Button) Findviewbyid (r.id.back); Back.setonclicklistener (</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Button.onclicklistener () {@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnClick (View arg0) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">TODO auto-generated Method Stub</span></span>String a=<span style="color: #000000;"><span style="color: #000000;">age.gettext (). toString (); <strong>Intent Intent</strong> </span></span><strong><strong>=<span style="color: #0000ff;">new</span> <span style="color: #000000;"> Intent (); Bundle Bundle</span>=<span style="color: #0000ff;">new</span> <span style="color: #000000;"> bundle (); Bundle.putstring (</span>"age"</strong></strong><span style="color: #000000;"><span style="color: #000000;"> <strong>, a); Intent.putextras (bundle); Setresult (result_code, intent); Finish ();</strong> } }); Intent Intent</span></span>=<span style="color: #000000;"><span style="color: #000000;">getintent (); String na</span></span>=intent.getstringextra ("name"<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">toast.maketext (second.this, na, toast.length_short). show ();</span></span><span style="color: #000000;"><span style="color: #000000;">Name.settext (na); }</span></span></pre><p><p></p></p><p><p> Data exchange between Android two activity </p> </p></span>

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.