重溫ActivityCollector/BaseActivity隨時(shí)退出和啟動(dòng)程序
掃描二維碼
隨時(shí)隨地手機(jī)看文章
ActivityCollector.java
package?sunny.example.comeonbroadcastreceiverdo; import?android.app.Activity; import?java.util.List; import?java.util.ArrayList; public?class?ActivityCollector?{ public?static?ListactivitiesList?=?new?ArrayList(); public?static?void?addAtivity(Activity?activity){ activitiesList.add(activity); } public?static?void?removeActivity(Activity?activity){ activitiesList.remove(activity); } public?static?void?finishAll(){ for(Activity?activity:activitiesList){ if(!activity.isFinishing()){ activity.finish(); } } } }
BaseActivity.java
package?sunny.example.comeonbroadcastreceiverdo; import?android.app.Activity; import?android.os.Bundle; public?class?BaseActivity?extends?Activity{ @Override protected?void?onCreate(Bundle?savedInstanceState){ super.onCreate(savedInstanceState); ActivityCollector.addAtivity(this); } @Override protected?void?onDestroy(){ super.onDestroy(); ActivityCollector.removeActivity(this); } }
使用:
public?class?MainActivity?extends?BaseActivity{ protected?void?onCreate(Bundle?savedInstanceState){ //BaseActivity中的onCreate() super.onCreate(savedInstanceState); ........ ?public?void?onClick(View?view){ ????????ActivityCollector.finishAll(); } ..... ?}}