To clear all background activities I did the following:
I kept a static array list, and whenever I used to go from one activity to another, in the onCreate() method of new activity, I added the object of current activity into that list like this:
SomeClass.addActivity(CurrentActivity.this);
I added the above statement in each activity.
The addActivity():
public void addActivity(final Activity activity) {
activityList.add(activity);
}
And when I wanted to clear the stack, I called:
public boolean clearStack() {
for (Activity activity : activityList) {
activity.finish();
}
activityList.clear();
return (activityList.isEmpty());
}
In this way, I cleared my activity stack.
But it produced a memory leak. It's not the right way to do that. It's not ok to hold references to activities. Can you guys explain me why and how exactly memory leak occurred in this case?
I used MAT for eclipse to find this memory leak in my app.
Any help would greatly be appreciated.