When a root activity is finishing and it is supposed to return to
home make sure there are only lower activities waiting to start before
going home.
Fixes bug 8632206.
Various other refactorings for efficiency.
Change-Id: I8bbb9de78d0ea9f45a504cf4bad72c698e9cc3d8