李国帅,编于年后
新年好,今天头天上班,继续整理,把以前的东西提取成一个个小文件。
2018/2/6 14:39:47
现在获知的另一种方式退出所有的activity,使用广播的方式,避免判断activity是否存在可能导致的错误。
以前使用每一次添加时记录,每次销毁时一条记录,然后在程序退出的时候,遍历列表进行销毁。
public class BaseActivity extends Activity { private BroadcastReceiver recvExit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MyApplication.getActivityList().addActivity(this);//方式1 以前的方式--记录和遍历 recvExit = new BroadcastReceiver() {//方式2 新的方式-广播 @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals("EXIT_APP")){ finish(); } } }; IntentFilter filter = new IntentFilter(); filter.addAction("EXIT_APP"); registerReceiver(recvExit, filter); } @Override protected void onDestroy() { //MyLog.i(TAG, " destroy!"); MyApplication.getActivityList().removeActivity(this);//方式1 try { unregisterReceiver(recvExit);//方式2 }catch (Exception e){ } super.onDestroy(); // Always call the superclass } } 销毁的时候调用函数 public void finishAll() { //方式1 for (Activity activity : activityList) { if (activity != null) { activity.finish(); } } } public void finishAll() {//方式2 Intent intent = new Intent("EXIT_APP"); MyApplication.getAppContext().sendBroadcast(intent); }