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 Accounts and Sync
in Android Settings, removes the account and signs in (still being in Accounts and Sync
as a different user.
I have defined a receiver for LOGIN_ACCOUNTS_CHANGED
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).
So, the question is: what should be done about these orphan activities?
clearTaskOnLaunch
, but all activities are in the background when the change happens. onResume()
of each activity, then launch clearTask
activity if needed? Too messy. android.os.Process.killProcess(android.os.Process.myPid())
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 LAUNCHER
activity, with clear history.So, what would be the best way to respond to the described scenario?
Have all your activities extend from a MyActivity
class which has a BroadcastReceiver
member: mChangeReceiver.
Have MyActivity
register mChangeReceiver in onCreate
(and unregister in onDestroy
), to the LOGIN_ACCOUNTS_CHANGED
intent.
Have the mChangeReceiver call some abstract method onAccountChanged()
that all extending classes need to override and implement to reflect the change in the GUI.
That's it.
Now, whenever the account changes, all your living activities will get their onAccountChanged
method called and will refresh their GUI.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With