Base activity
public class BaseActivity extends FragmentActivity{
private Dialog loadingDialog;
protected List<AsyncTask> tasks;
protected List<Toast> toasts;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tasks = new LinkedList<AsyncTask>();
toasts = new LinkedList<Toast>();
}
@Override
protected void onPause() {
super.onPause();
isVisible = false;
this.hideLoadingDialog();
this.cancelAllTasks();
this.cancelAllToasts();
}
private void cancelAllToasts() {
for (Toast currentToast : toasts) {
currentToast.cancel();
}
toasts.clear();
}
public void addTask(AsyncTask task) {
tasks.add(task);
}
private void cancelAllTasks() {
for (AsyncTask currentTask : tasks) {
if (currentTask != null && currentTask.getStatus() != Status.FINISHED) {
currentTask.cancel(true);
}
}
tasks.clear();
}
@Override
public void showLoadingDialog() {
if (loadingDialog == null) {
// Probably should have a reference counter here
loadingDialog = DialogHelper.buildLoadingDialog(this, this.getString(R.string.loading_dialog_loading_message));
loadingDialog.show();
}
}
@Override
public void hideLoadingDialog() {
if (loadingDialog != null) {
// And here we decrement that counter and dismiss the dialog if it reaches 0
loadingDialog.dismiss();
loadingDialog = null;
}
}
@Override
public void onDestroy() {
super.onDestroy();
MemoryManagementHelper.unbindDrawables(((ViewGroup) findViewById(android.R.id.content)).getChildAt(0));
System.gc();
}
}
MemoryManagementHelper
public class MemoryManagementHelper {
public static void unbindDrawables(View view) {
if (view != null) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup && !(view instanceof AdapterView)) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
} else {
// TODO - ?
}
}
}
No hay comentarios:
Publicar un comentario