Android intent three ways to pass the value

Source: Internet
Author: User
<span id="Label3"></p><p><p>The three methods of intent of Android Are: 1, startactivity (), 2, startactivityforresult (), 3, Calling the method "actionstart ()" in the next activity customization, That's the best way.</p></p><p><p>first, the general way to value Jump: startactivity () method</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-comment">/* 在MainActivity中定义如下代码 */</span>button1.setOnClickListener(<span class="hljs-keyword">new</span> OnClickListener() { <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onClick</span>(View v) { <span class="hljs-keyword">new</span> Intent(MainActivity.<span class="hljs-keyword">this</span>,TwoActivity.class); <span class="hljs-comment">//在Intent对象当中添加一个键值对</span> _intent.putExtra(<span class="hljs-string">"key"</span>,<span class="hljs-string">"value"</span>); startActivity(_intent); }});</code></pre></pre><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">twoactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span> {</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Requestwindowfeature (window.feature_no_title); Setcontentview (r.layout.second_layout); Button button2 = (button) Findviewbyid (r.id.button_2); Button2.setonclicklistener (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Onclicklistener () {<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnClick</span></span>(View V) {<span class="hljs-comment"><span class="hljs-comment">//get The intent object passed from the previous Activity.</span></span>Intent _intent = getintent ();<span class="hljs-comment"><span class="hljs-comment">//from Intent to get value from key</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(_intent! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {String _value = _intent.getstringextra (<span class="hljs-string"><span class="hljs-string">"key"</span></span>); Toast.maketext (twoactivity.<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>, _value, toast.length_short). Show (); } } }); }}</code></pre><p><p>second, callback data Transfer value Jump: Startactivityforresult () method</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-comment">/ <span class="hljs-comment">* Define the following code in Mainactivity *</span> /</span>Button1.setonclicklistener (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Onclicklistener () {<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnClick</span></span>(View V) {Intent _intent =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Intent (mainactivity.<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>, twoactivity.class); _intent.putextra (<span class="hljs-string"><span class="hljs-string">"key"</span></span>,<span class="hljs-string"><span class="hljs-string">"value"</span></span>);<span class="hljs-comment"><span class="hljs-comment">///the second parameter is the request code, as long as it is a unique value</span></span>Startactivityforresult (_intent,<span class="hljs-number"><span class="hljs-number">1234</span></span>); }});<span class="hljs-comment"><span class="hljs-comment">//since We are using the Startactivityforresult () method to start the twoactivity</span></span> <span class="hljs-comment"><span class="hljs-comment">//the Onactivityresult () method of the previous activity is recalled after Twoactivity is destroyed</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Onactivityresult</span></span>(<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>requestcode,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>resultcode, Intent Intent) {<span class="hljs-keyword"><span class="hljs-keyword">Switch</span></span>(requestcode) {<span class="hljs-keyword"><span class="hljs-keyword"></span> case</span> <span class="hljs-number"><span class="hljs-number">1234</span></span>:<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(resultcode = = Result_ok) {String Returneddata = Intent.getstringextra (<span class="hljs-string"><span class="hljs-string">"key1"</span></span>); Toast.maketext (mainactivity.<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>, returneddata, toast.length_short). Show (); }<span class="hljs-keyword"><span class="hljs-keyword"></span> break</span>;<span class="hljs-keyword"><span class="hljs-keyword">default</span></span>: } }</code></pre><p><p>The Onactivityresult () method has three parameters, the first parameter requestcode, which is the request code that we passed in when the activity was Started. The second parameter, resultcode, is that we pass in the result code when we return the Data. The third parameter, data, is the intent that carries the returned Data. Since it is possible to invoke the Startactivityforresult () method in an activity to initiate many different activities, the data returned by each activity is recalled to the Onactivityresult () method, So the first thing we need to do is to judge the data source by checking the value of the Requestcode. After determining that the data is returned from twoactivity, we then use the value of ResultCode to determine whether the processing result is Successful. finally, The value is taken from data and printed out, which completes the work of returning data to an Activity.</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">twoactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span> {</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Requestwindowfeature (window.feature_no_title); Setcontentview (r.layout.second_layout); Button button2 = (button) Findviewbyid (r.id.button_2);<span class="hljs-comment"><span class="hljs-comment">//receive data passed from Mainaactivity</span></span>Intent _intent = getintent ();<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(_intent! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {String _value = _intent.getstringextra (<span class="hljs-string"><span class="hljs-string">"key"</span></span>); Toast.maketext (twoactivity.<span class="hljs-keyword"><span class="hljs-keyword"></span> this</span>, _value, toast.length_short). Show (); } button2.setonclicklistener (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Onclicklistener () {<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">OnClick</span></span>(View V) {Intent Intent =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Intent (); Intent.putextra (<span class="hljs-string"><span class="hljs-string">"key1"</span></span>,<span class="hljs-string"><span class="hljs-string">"value" Activity "</span></span>);<span class="hljs-comment"><span class="hljs-comment">//dedicated to returning data to the previous activity</span></span> <span class="hljs-comment">the <span class="hljs-comment">first parameter is used to return the result code to the previous activity, typically using only the RESULT_OK or result_canceled values</span></span> <span class="hljs-comment"><span class="hljs-comment">//the second parameter is to pass the intent with the data back</span></span>Setresult (result_ok, intent);<span class="hljs-comment"><span class="hljs-comment">//call The Finish () method to destroy the current activity</span></span>Finish (); } }); }}</code></pre><p><p>Iii. the best wording for initiating activities</p></p><p><p>In real project development, there will always be problems of docking. For example, twoactivity is not developed by you, but now the part you are responsible for needs to have the ability to start twoactivity, but you do not know what data to start the activity to Pass. There are two ways, one is to read the code in twoactivity, and the other is to ask the colleague who is responsible for writing Twoactivity. Do you think it would be a hassle? In fact, only need to change the wording, you can easily solve the above Dilemma.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-comment">/* 在MainActivity中定义如下代码 */</span>button1.setOnClickListener(<span class="hljs-keyword">new</span> OnClickListener() { <span class="hljs-annotation">@Override</span> <span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onClick</span>(View v) { <span class="hljs-comment">/*最佳数据传值方法:调用在下个activity自定义的方法*/</span> TwoActivity.actionStart(MainActivity.<span class="hljs-keyword">this</span><span class="hljs-string">"data1"</span><span class="hljs-string">"data2"</span>); }});</code></pre></pre><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">twoactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span> {</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Requestwindowfeature (window.feature_no_title); Setcontentview (r.layout.second_layout); }<span class="hljs-comment">/ <span class="hljs-comment">* Best Data transfer method *</span> /</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">ACTIONStart</span></span>(context context, string data1, string data2) {Intent Intent =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Intent (context, twoactivity.class); Intent.putextra (<span class="hljs-string"><span class="hljs-string">"param1"</span></span>, data1); Intent.putextra (<span class="hljs-string"><span class="hljs-string">"param2"</span></span>, data2); Context.startactivity (intent); }}</code></pre><p><p>Once implemented, all data required in the twoactivity is passed through the parameters of the ACTIONStart () method, stored in intent, and the StartActivity () method is called to start Twoactivity.</p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android intent three ways to pass the value</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.