SysApplication这个类复制到工程里面,然后在每个Acitivity的oncreate方法里面通过SysApplication.getInstance().addActivity(this); 添加当前Acitivity到ancivitylist里面去,最后在想退出的时候调用SysApplication.getInstance().exit();可直接关闭所有的Acitivity并退出应用程序。 附代码: 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 mList = new LinkedList(); 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); } } public void onLowMemory() { super.onLowMemory(); System.gc(); } } 在应用程序里面 的activity的oncreate里面添加SysApplication.getInstance().addActivity(this) 如: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); SysApplication.getInstance().addActivity(this); } |
(转)退出所有activity
最新推荐文章于 2021-05-25 23:17:50 发布