public abstract class ActivityBase extends Activity {
public static final String TAG = "ActivityBase";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void onDestroy() {
if (_activityControllers != null) {
for (int i = 0; i < _activityControllers.size(); i++) {
ControllerBase child = _activityControllers.get(i);
child.internal_gc();
}
_activityControllers = null;
}
unregisterEventBus();
super.onDestroy();
}
protected void registerEventBus() {
isRegisterEventBus = true;
EventBus.getDefault().register(this);
}
protected void unregisterEventBus() {
if (isRegisterEventBus){
EventBus.getDefault().unregister(this);
isRegisterEventBus = false;
}
}
protected void addActivityController(ControllerBase child) {
if (_activityControllers == null) {
_activityControllers = new ArrayList<>();
}
_activityControllers.add(child);
}
protected List<ControllerBase> _activityControllers;
protected Boolean isRegisterEventBus = false;
}
public abstract class ControllerBase {
//子 Controller 由 父 Controller 管理
//子 Controller 在父 Controller 实例化
public ControllerBase(ControllerBase parent) {
parent.addChild(this);
}
//附着 activity
public ControllerBase(ActivityBase activity) {
activity.addActivityController(this);
}
//do not directly call it, it will call by internal_gc() when activity destroyed
abstract protected void gc();
//call by ActivityBase when it destroyed
protected void internal_gc() {
if (_childController != null) {
for (int i = 0; i < _childController.size(); i++) {
ControllerBase child = _childController.get(i);
child.internal_gc();
}
_childController = null;
}
gc();
}
private void addChild(ControllerBase child) {
if (_childController == null) {
_childController = new ArrayList<>();
}
_childController.add(child);
}
List<ControllerBase> _childController;
}