Jump and data transfer between Android activity

Source: Internet
Author: User
<span id="Label3"></p>1. Jump between activity and transfer data<p><p>A activity-performed operation</p></p><pre><pre> <span style="color: #0000ff">New</span> Intent (context, B.<span style="color: #0000ff">class</span><span style="color: #000000">); Intent.putextra (</span>"tn"<span style="color: #000000">, tn); Intent.putextra (</span>"paymode"<span style="color: #000000">, paymode); Context.startactivity (intent);</span></pre></pre><p><p>B activity receives data</p></p><pre><pre> <span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span> <span style="color: #000000">onCreate (Bundle Savedinstancestate) { </span><span style="color: #0000ff">Super</span><span style="color: #000000">. OnCreate (savedinstancestate); </span> =<span style="color: #000000"> getintent (). getextras (); </span> = extras.getstring ("tn"<span style="color: #000000">); </span> = Extras.getstring ("paymode");</pre></pre><p><p></p></p>2, Startactivityforresult () Usage<p><p>B activity waits for the result of C 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_b); Intent Intent</span></span>=<span style="color: #0000ff"><span style="color: #0000ff">New</span></span>Intent (B.<span style="color: #0000ff"><span style="color: #0000ff"></span> this</span>C.<span style="color: #0000ff"><span style="color: #0000ff">class</span></span><span style="color: #000000"><span style="color: #000000">); Startactivityforresult (intent,</span></span>10<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>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: #0000ff"><span style="color: #0000ff">Super</span></span><span style="color: #000000"><span style="color: #000000">. Onactivityresult (requestcode, resultcode, data); String A</span></span>= Data.getstringextra ("a"<span style="color: #000000"><span style="color: #000000">); String B</span></span>= Data.getstringextra ("b"<span style="color: #000000"><span style="color: #000000">); </span></span><span style="color: #0000ff"><span style="color: #0000ff">if</span></span>(resultcode = = 9<span style="color: #000000"><span style="color: #000000">) {log.d (TAG,</span></span>"onactivityresult:" +a+ "" +<span style="color: #000000"><span style="color: #000000">B); } }</span></span></pre><p><p>C activity transmits data to B</p></p><pre><pre><span style="color: #0000ff">protected</span> <span style="color: #0000ff">void</span> <span style="color: #000000">onCreate (Bundle Savedinstancestate) { </span><span style="color: #0000ff">Super</span><span style="color: #000000">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_c); </span> <span style="color: #0000ff">New</span> <span style="color: #000000">Intent (); Mintent.putextra (</span>"a", "namea"<span style="color: #000000">); Mintent.putextra (</span>"b", "nameb"<span style="color: #000000">); </span> <span style="color: #0000ff"></span>this. setresult (9<span style="color: #000000">, mintent); }</span></pre></pre><p><p></p></p>3, the above application:<p><p>Scene:</p></p><p><p>When connecting to UnionPay payment: The page that initiates the payment must write a Onactivityresult () function to receive the result of the Payment. Now you don't want to rewrite the function on the page that originated the payment, but to integrate it into the sdk,</p></p><p><p>What we need to do is set up an intermediary activity to initiate the Payment. A------C</p></p><p><p>Now the Problem: 1, set up an intermediate activity will have a jump effect, the user will inexplicably jump into a blank activity and then jump to the payment Page.</p></p><p><p>2, return, You need to press two times return to return to the original interface.</p></p><p><p></p></p><p><p>Workaround:</p></p><p><p>Initiating page a, Intermediate page B, Payment page C</p></p><p><p>1, B Set Registration when the activity is transparent, so jump will not have a splash screen feeling.</p></p><p><p>Android:theme= "@android: style/theme.translucent"</p></p><p><p>2. When the corresponding operation is completed in Onactivityresult (), call finish ();</p></p><p><p>You do not need to press back again to exit the intermediate activity</p></p><p><p>Problem solved Perfectly.</p></p><p><p></p></p><p><p>Another way of thinking proves to be wrong.</p></p><p><p>After initiating the payment in B, call finish () directly; This jumps to the page <span style="font-family: "Microsoft YaHei"">without</span> <span style="font-size: 14px"> <span style="font-family: "Microsoft YaHei"">jumping to a blank page, and</span> does not need to be adjusted two times when returning from the payment page</span> .</p></p><p><p>however, the Onactivityresult () method in B will not be called.</p></p><p><p></p></p>4. Differences between the finish () and OnDestroy () methods in Activity: activity.finish ()<span style="font-family: 宋体">Call the When the your activity is do and <span style="font-family: 宋体"> <span style="font-size: 14px; color: #000000">should be Closed.</span> </span></span><p><p><span style="font-family: 宋体; color: #000000"><span style="font-size: 14px"> <span style="font-family: "Microsoft YaHei""></span> </span> </span> <span style="font-family: "Microsoft YaHei""> <span style="font-size: 14px">Call this method when your activity is completed, or when the activity needs to be closed. </span> <span style="font-size: 14px">When you call this method, the system simply moves the top activity out of the stack,</span> </span></p></p><p><p><span style="font-family: "Microsoft YaHei""><span style="font-size: 14px">The Ondestory () method is not invoked in a timely manner, and the resources it occupies are not released in a timely manner. Since the stack is removed, you will not find this activityactivity when you click on the "back" button on your phone.</span></span></p></p><span style="font-family: "Microsoft YaHei""><span style="font-family: "Microsoft YaHei"">ondestory ()</span></span><p><p><span style="font-family: "Microsoft YaHei"">The system is temporarily destroying this instance of the activity to save Space.</span></p></p><p><p><span style="font-family: "Microsoft YaHei"">The system destroys the space occupied by the instance of the activity in Memory. During the activity life cycle, the Ondestory () method is the last step of his life, and the resource space is Recycled.</span></p></p><p><p><span style="font-family: "Microsoft YaHei"">When you re-enter this activity, you must recreate and execute the OnCreate () method.</span></p></p><p><p></p></p><p><p>5. Manage open activities by defining a Activitycollecter class</p></p><pre><span style="color: #0000ff"><span style="color: #0000ff"></span> package</span><span style="color: #000000"><span style="color: #000000">com.example.activitytest;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">android.app.Activity;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">java.util.ArrayList;</span></span><span style="color: #0000ff"><span style="color: #0000ff">Import</span></span><span style="color: #000000"><span style="color: #000000">java.util.List;</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">class</span></span><span style="color: #000000"><span style="color: #000000">Activitycollector {</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">Static</span></span>list<activity> activities =<span style="color: #0000ff"><span style="color: #0000ff">New</span></span>Arraylist<><span style="color: #000000"><span style="color: #000000">(); </span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">Static</span></span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">addactivity (activity activity) {activities.add (activity); } </span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">Static</span></span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">removeactivity (activity activity) {activities.remove (activity); } </span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> public</span> <span style="color: #0000ff"><span style="color: #0000ff">Static</span></span> <span style="color: #0000ff"><span style="color: #0000ff">void</span></span><span style="color: #000000"><span style="color: #000000">Finishall () {</span></span><span style="color: #0000ff"><span style="color: #0000ff"></span> for</span><span style="color: #000000"><span style="color: #000000">(Activity activity:activities) {</span></span><span style="color: #0000ff"><span style="color: #0000ff">if</span></span>(!<span style="color: #000000"><span style="color: #000000">activity.isfinishing ()) {activity.finish (); } } }}</span></span></pre><p><p>Add when each activity is created:</p></p><pre><pre>Activitycollector.addactivity (<span style="color: #0000ff"></span>this);</pre></pre><p><p>When this is called in the last activity, the activity is emptied</p></p><pre><pre><span style="color: #000000">Activitycollector.finishall (); Android.os.Process.killProcess (android.os.Process.myPid ());</span></pre></pre><p><p></p></p><p><p>Activity Life Cycle:</p></p><p><p>Http://www.cnblogs.com/NeilZhang/p/6898140.html</p></p><p><p>GitHub Address:</p></p><p><p>[email Protected]:lsfzlj/androidtestproject.git</p></p><p><p></p></p><p><p>Jump and data transfer between Android 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.