ActivityCollector.java
package sunny.example.comeonbroadcastreceiverdo;
import android.app.Activity;
import java.util.List;
import java.util.ArrayList;
public class ActivityCollector {
public static List<Activity> activitiesList = new ArrayList<Activity>();
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();
}
.....
}}