为了实现Activity管理,我们可以新建CollectionActivity类,这里面有三个静态方法,一个静态变量List.一个add一个remove一个finnishall()
建立一个BaseActivity继承Activity,重写oncreate(),里面Log一下当前activity的name俩方法分别是getClass().getSimpleName();还有getLocalClassName();为了方便你知道自己随时知道自己在哪个活动.
代码
public class CollectionActivity {
public static List<Activity> activityList = new ArrayList<Activity>();
public static void addActivity(Activity activity) {
activityList.add(activity);
}
public static void removeActivity(Activity activity) {
activityList.remove(activity);
}public static void finishAll(){
for(int i= 0;i < activityList.size();i++)
{
if (!activityList.get(i) .isFinishing()){
activityList.get(i).finish(); }
}
}
baseActivity
public class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.d("lkk", this.getLocalClassName());
Log.d("lkk",this.getClass().getSimpleName());
CollectionActivity.addActivity(this);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
CollectionActivity.removeActivity(this);
}
到此关于Activity差不多了解了.,但是心中还是有疑惑的,总结写.关于context我只知道是activty的父类,但是啥时候用mainActivity.class.啥时候用this.啥时候用MainActivity,this.感觉分不清楚有时候.