欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 中 退出多个activity的经典方法

程序员文章站 2024-03-05 10:44:18
1.使用list集合方式 用list保存activity实例,然后逐一干掉 import java.util.linkedlist; import jav...

1.使用list集合方式

用list保存activity实例,然后逐一干掉

import java.util.linkedlist;
import java.util.list;
import android.app.activity;
import android.app.alertdialog;
import android.app.application;
import android.content.dialoginterface;
import android.content.intent;
public class sysapplication extends application {
private list<activity> mlist = new linkedlist<activity>();
private static sysapplication instance;
private sysapplication() {
}
public synchronized static sysapplication getinstance() {
if (null == instance) {
instance = new sysapplication();
}
return instance;
}
// add activity
public void addactivity(activity activity) {
mlist.add(activity);
}
public void exit() {
try {
for (activity activity : mlist) {
if (activity != null)
activity.finish();
}
} catch (exception e) {
e.printstacktrace();
} finally {
// system.exit(0);//去掉这个
}
}
@override
public void onlowmemory() {
super.onlowmemory();
system.gc();
}
}

在每个activity的oncreate方法中添加类似代码:

public void oncreate(bundle savedinstancestate) {
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肯定退出。

代码如下:

import android.content.broadcastreceiver;
import android.content.context;
import android.content.intent;
import android.content.intentfilter;
import android.support.v7.app.appcompatactivity;
public class baseactivity extends appcompatactivity {
protected broadcastreceiver broadcastreceiver = new broadcastreceiver() {
@override
public void onreceive(context context, intent intent) {
finish();
}
};
@override
public void onresume() {
super.onresume();
// 在当前的activity中注册广播
intentfilter filter = new intentfilter();
filter.addaction("exitapp");
this.registerreceiver(this.broadcastreceiver, filter);
}
@override
protected void ondestroy() {
// todo auto-generated method stub
super.ondestroy(); //解除绑定
this.unregisterreceiver(this.broadcastreceiver);
}
}

  一个activity实例:

import android.content.intent;
import android.support.v7.app.appcompatactivity;
import android.os.bundle;
import android.util.log;
import android.view.keyevent;
import android.view.view;
import android.widget.button;
import android.widget.toast;
public class activityc extends baseactivity {
button btn;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_activity_c);
sysapplication.getinstance().addactivity(this);
btn = (button) findviewbyid(r.id.acticityc_btn);
//关闭所有的activity
btn.setonclicklistener(new view.onclicklistener() {
@override
public void onclick(view view) {
}
});
}
/**
* 捕获手机物理菜单键
*/
private long exittime = 0;
@override
public boolean onkeydown(int keycode, keyevent event) {
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();
}
return true;
}
return super.onkeydown(keycode, event);
}
protected void myexit() {
//第二种退出方法
// sysapplication.getinstance().exit();
intent intent = new intent();
intent.setaction("exitapp");
this.sendbroadcast(intent);
//super.finish();
}
@override
protected void onstop() {
super.onstop();
log.i("tag","activityc-onstop");
}
@override
protected void ondestroy() {
super.ondestroy();
log.i("tag","activityc-ondestroy");
}
}

  就是在退出的地方调用这个:

private long exittime = 0;
@override
public boolean onkeydown(int keycode, keyevent event) {
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();
}
return true;
}
return super.onkeydown(keycode, event);
}
protected void myexit() {
//第二种退出方法
// sysapplication.getinstance().exit();
intent intent = new intent();
intent.setaction("exitapp");
this.sendbroadcast(intent);
//super.finish();
}

  优点:方便,方便!!只需要在退出的地方发送广播就可以!!

这是我的退出时的输出:

08-09 15:33:48.869 26919-26919/example.com.closeapp i/tag: mainactivity-onpause
08-09 15:33:49.279 26919-26919/example.com.closeapp i/tag: mainactivity-onstop
08-09 15:33:51.569 26919-26959/example.com.closeapp d/openglrenderer: endallstaginganimators on 0x55850598b0 (rippledrawable) with handle 0x5585041510
08-09 15:33:56.079 26919-26919/example.com.closeapp i/tag: mainactivity-ondestroy
08-09 15:33:56.089 26919-26919/example.com.closeapp i/tag: activityb-ondestroy
08-09 15:33:56.399 26919-26919/example.com.closeapp i/tag: activityc-ondestroy 

以上所述是小编给大家介绍的android 中 退出多个activity的经典方法,希望对大家有所帮助