重温ActivityCollector/BaseActivity随时退出和启动程序
扫描二维码
随时随地手机看文章
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(); } ..... }}