In Android development, close another activity in one activity

Source: Internet
Author: User

In Android development, close another activity in one activityFor example, there are Activitya, Activityb, close Activitya in Activityb .
Solution: 1.
In Activitya, set a static variable instance, initialized to this
Inside the Activityb, ActivityA.instance.finish ();   2. can also be Activitymanager Activitymanager manager = (Activitymanager) getsystemservice (Activity_service);  Manager.restartpackage (PackageName); Can join:AndroidDevelopment Total CommunicationGroup: 179730949 (500 people) or Android development Total Exchange Group: 249919939 (1000 people)Examples are as follows:ActivityaPackage Com.activity.yuzhenbei;import Android.os.bundle;import Android.app.activity;import android.content.Intent; Import Android.view.keyevent;import Android.view.menu;import Android.view.menuitem;public class MainActivity extends Activity {Public static Activitya instance = null;@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activitya);instance = this;        intent Intent = new Intent (); Intent.setclass (Activitya.this, Activityb.class); ActivityA.this.startActivity (Intent);    }         menu  //When you click the menu button, call the method    @Override   public boolean oncreateoptionsmenu (Menu menu ) {  menu.add (0, 1, 1, r.string.help). SetIcon (  android. R.drawable.ic_menu_close_clear_cancel)   return super.oncreateoptionsmenu (menu);  }    //Select a dish    @Override   public boolean onoptionsitemselected (MenuItem item) {   if (item.getitemid () = = 1) {  intent Intent = new Intent (); Intent.setclass (Activitya.this, Activityb.class); ActivityA.this.startActivity (intent);   }  return super.onoptionsitemselected (item);   }        //return key    @Override   public Boolean OnKeyDown (int KeyCode, KeyEvent event) {  if (keycode = = Keyevent.keycode_back) {//If it is a return key on the phone    ActivityA.this.finish ();   }  return Super.onkeydown (KeyCode, event);  }} ActivitybPackage Com.activity.yuzhenbei;import Android.app.activity;import Android.os.bundle;import android.view.KeyEvent; Import Android.view.view;public class Activityb extends Activity {@Overridepublic void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.LAYOUT.ACTIVITYB);} Return key @overridepublic boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_back) {//If it is returned on the phone Key ActivityB.this.finish ();ActivityA.instance.finish ();}return Super.onkeydown (KeyCode, event);}}

In Android development, close another activity in one activity

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.