Android 中 退出多个activity的经典方法
1.使用List集合方式
用list保存activity实例,然后逐一干掉
importjava.util.LinkedList; importjava.util.List; importandroid.app.Activity; importandroid.app.AlertDialog; importandroid.app.Application; importandroid.content.DialogInterface; importandroid.content.Intent; publicclassSysApplicationextendsApplication{ privateList<Activity>mList=newLinkedList<Activity>(); privatestaticSysApplicationinstance; privateSysApplication(){ } publicsynchronizedstaticSysApplicationgetInstance(){ if(null==instance){ instance=newSysApplication(); } returninstance; } //addActivity publicvoidaddActivity(Activityactivity){ mList.add(activity); } publicvoidexit(){ try{ for(Activityactivity:mList){ if(activity!=null) activity.finish(); } }catch(Exceptione){ e.printStackTrace(); }finally{ //System.exit(0);//去掉这个 } } @Override publicvoidonLowMemory(){ super.onLowMemory(); System.gc(); } }
在每个Activity的onCreate方法中添加类似代码:
publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); SysApplication.getInstance().addActivity(this); }
在需要退出程序的时候,调用:
SysApplication.getInstance().exit();
简而言之,通过单例模式把每个Activity的引用添加到一个全局链表中,每次退出程序调用System.exit(0)时,先调用链表中Activity的finish方法
优点:实现简单,逻辑清晰
2.使用广播
推荐!!好方便!!
基本逻辑就是:定义一个BaseActivity,在这个Activity中绑定广播,在广播的onReceive方法中调用finish();然后以后的Activity都继承这个Activity,退出时发送广播,退出BaseActivity,父activity都退出了,子activity肯定退出。
代码如下:
importandroid.content.BroadcastReceiver; importandroid.content.Context; importandroid.content.Intent; importandroid.content.IntentFilter; importandroid.support.v7.app.AppCompatActivity; publicclassBaseActivityextendsAppCompatActivity{ protectedBroadcastReceiverbroadcastReceiver=newBroadcastReceiver(){ @Override publicvoidonReceive(Contextcontext,Intentintent){ finish(); } }; @Override publicvoidonResume(){ super.onResume(); //在当前的activity中注册广播 IntentFilterfilter=newIntentFilter(); filter.addAction("ExitApp"); this.registerReceiver(this.broadcastReceiver,filter); } @Override protectedvoidonDestroy(){ //TODOAuto-generatedmethodstub super.onDestroy();//解除绑定 this.unregisterReceiver(this.broadcastReceiver); } }
一个Activity实例:
importandroid.content.Intent; importandroid.support.v7.app.AppCompatActivity; importandroid.os.Bundle; importandroid.util.Log; importandroid.view.KeyEvent; importandroid.view.View; importandroid.widget.Button; importandroid.widget.Toast; publicclassActivityCextendsBaseActivity{ Buttonbtn; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_activity_c); SysApplication.getInstance().addActivity(this); btn=(Button)findViewById(R.id.acticityc_btn); //关闭所有的activity btn.setOnClickListener(newView.OnClickListener(){ @Override publicvoidonClick(Viewview){ } }); } /** *捕获手机物理菜单键 */ privatelongexitTime=0; @Override publicbooleanonKeyDown(intkeyCode,KeyEventevent){ if(keyCode==KeyEvent.KEYCODE_BACK){//&&event.getAction()==KeyEvent.ACTION_DOWN if((System.currentTimeMillis()-exitTime)>2000){ Toast.makeText(getApplicationContext(),"再按一次退出程序",Toast.LENGTH_SHORT).show(); exitTime=System.currentTimeMillis(); }else{ myExit(); } returntrue; } returnsuper.onKeyDown(keyCode,event); } protectedvoidmyExit(){ //第二种退出方法 //SysApplication.getInstance().exit(); Intentintent=newIntent(); intent.setAction("ExitApp"); this.sendBroadcast(intent); //super.finish(); } @Override protectedvoidonStop(){ super.onStop(); Log.i("tag","ActivityC-onStop"); } @Override protectedvoidonDestroy(){ super.onDestroy(); Log.i("tag","ActivityC-onDestroy"); } }
就是在退出的地方调用这个:
privatelongexitTime=0; @Override publicbooleanonKeyDown(intkeyCode,KeyEventevent){ if(keyCode==KeyEvent.KEYCODE_BACK){//&&event.getAction()==KeyEvent.ACTION_DOWN if((System.currentTimeMillis()-exitTime)>2000){ Toast.makeText(getApplicationContext(),"再按一次退出程序",Toast.LENGTH_SHORT).show(); exitTime=System.currentTimeMillis(); }else{ myExit(); } returntrue; } returnsuper.onKeyDown(keyCode,event); } protectedvoidmyExit(){ //第二种退出方法 //SysApplication.getInstance().exit(); Intentintent=newIntent(); intent.setAction("ExitApp"); this.sendBroadcast(intent); //super.finish(); }
优点:方便,方便!!只需要在退出的地方发送广播就可以!!
这是我的退出时的输出:
08-0915:33:48.86926919-26919/example.com.closeappI/tag:MainActivity-onPause 08-0915:33:49.27926919-26919/example.com.closeappI/tag:MainActivity-onStop 08-0915:33:51.56926919-26959/example.com.closeappD/OpenGLRenderer:endAllStagingAnimatorson0x55850598b0(RippleDrawable)withhandle0x5585041510 08-0915:33:56.07926919-26919/example.com.closeappI/tag:MainActivity-onDestroy 08-0915:33:56.08926919-26919/example.com.closeappI/tag:ActivityB-onDestroy 08-0915:33:56.39926919-26919/example.com.closeappI/tag:ActivityC-onDestroy
以上所述是小编给大家介绍的Android中退出多个activity的经典方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!