How should an Android application respond to critical changes in the environment?
<p>Let's say there's an application that creates an account in AccountManager. User explores the quite complicated activity graph of this application for a while, then goes to <code>Accounts and Sync</code> in Android Settings, removes the account and signs in (still being in <code>Accounts and Sync</code> as a different user.</p>

<p>I have defined a receiver for <code>LOGIN_ACCOUNTS_CHANGED</code> broadcast and I'm able to shut down all services gracefully. But activities are still there, bearing the name of the first user in their header (UI gets messed up in a number of ways, but this one is the most obvious).</p>

<p>So, the question is: what should be done about these orphan activities? </p>

<ul>
<li>I could use something like <code>clearTaskOnLaunch</code>, but all activities are in the background when the change happens. </li>
<li>Set a flag in SharedPreferences and check in <code>onResume()</code> of each activity, then launch <code>clearTask</code> activity if needed? Too messy. </li>
<li>The best option I was able to come up with is to use <code>android.os.Process.killProcess(android.os.Process.myPid())</code> to kill all activities. This is not too graceful, but gets job done. The only side effect is that activity stack is still there, when the most reasonable thing seems to be to start from the <code>LAUNCHER</code> activity, with clear history.</li>
</ul>

<p>So, what would be the best way to respond to the described scenario?</p>
