What is wrong with this code when I want to close all activities in a task?
public static void closeAllBelowActivities(Activity current)
{
boolean flag = true;
Activity below = current.getParent();
if (below == null)
return;
System.out.println("Below Parent: " + below.getClass());
while (flag) {
Activity temp = below;
try {
below = temp.getParent();
temp.finish();
} catch (Exception e) {
flag = false;
}
}
}
The way android works is that activities live in a stack (which you may know) so if A calls B calls C calls D
The stack looks like
If all you are trying to do is make sure that C,B,A are not in the stack anymore then you should call finish() before calling the next activity
And so on, so I guess the my next question is why are you trying to do that via the current activity vs. from the calling activity which is the way it was designed?
You can also use intent flags like FLAG_ACTIVITY_CLEAR_TOP to clear the history list of all but the newest activity, which maybe is what you are trying to do?