Android shuts down another activity in one activity

Source: Internet
Author: User
<span id="Label3"></p><p><p><span style="font-size: 15px;">For example, there are activitya, activityb, close Activitya in activityb.</span></p></p><p><p><span style="font-size: 15px;">Solution:</span></p></p><p><p><span style="font-size: 15px;">1. In activitya, set a static variable instance, initialized to this, in activityb, ActivityA.instance.finish ();</span></p></p><p><p><span style="font-size: 15px;">2. You can also use Activitymanager</span></p></p><span style="font-size: 15px;"><span style="font-size: 15px;">Activitymanager manager = (activitymanager) Getsystemservice (activity_service); </span></span><span style="font-size: 15px;"><span style="font-size: 15px;">manager.restartpackage (packagename)</span></span><p><p><span style="font-size: 15px;">Code:</span></p></p><p><p><span style="font-size: 15px;"><strong>Activitya:</strong></span></p></p><span style="font-size: 15px;">public <span style="font-size: 15px;">class Mainactivity extends Activity {</span></span><span style="font-size: 15px;">public <span style="font-size: 15px;"> <span style="color: #ff0000;">static Activitya instance = null;</span> </span></span><span style="font-size: 15px;"><span style="font-size: 15px;">@Override</span></span><span style="font-size: 15px;">public <span style="font-size: 15px;">void OnCreate (Bundle Savedinstancestate) {</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">super.oncreate (savedinstancestate);</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">Setcontentview (r.layout.activitya);</span></span><span style="font-size: 15px;"><span style="font-size: 15px;"> <span style="color: #ff0000;">instance = this;</span> </span></span><span style="font-size: 15px;"><span style="font-size: 15px;">Intent Intent = new Intent ();</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">intent.setclass (activitya.this, activityb.class);</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">ActivityA.this.startActivity (intent);</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">    }</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">}</span></span><span style="font-size: 15px;"><span style="font-size: 15px;"> </span></span><span style="font-size: 15px;"><span style="font-size: 15px;"><strong>activityb:</strong></span></span><span style="font-size: 15px;">public <span style="font-size: 15px;">class Activityb extends Activity {</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">@Override</span></span><span style="font-size: 15px;">public <span style="font-size: 15px;">void OnCreate (Bundle Savedinstancestate) {</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">super.oncreate (savedinstancestate);</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">Setcontentview (r.layout.activityb);</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">}</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">//return Key</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">@Override</span></span><span style="font-size: 15px;">public <span style="font-size: 15px;">boolean onKeyDown (int keycode, keyevent event) {</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">if (keycode = = Keyevent.keycode_back) {//if It is a return key on the phone</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">ActivityB.this.finish ();</span></span><span style="color: #ff0000; font-size: 15px;"><span style="color: #ff0000; font-size: 15px;">ActivityA.instance.finish ();</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">}</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">return super.onkeydown (keycode, event);</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">}</span></span><span style="font-size: 15px;"><span style="font-size: 15px;">}</span></span><p><p>Android shuts down another activity in one 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.