title: How to build AccountManager around Android killing the app process
body: <p>I have an app where users can log in and browse the app through several activities. To do that, I have an <code>AccountManager</code> set up in my main activity, the one that is always at the bottom of the activity stack - let's call it <code>MainActivity</code>. There are other activities that depend on the user being logged in, and that use the <code>AccountManager</code> instance that's in <code>MainActivity</code>.</p>
  
  <p>Assuming a user is in an activity stack with such an activity <code>ActivityB</code> on top, and <code>MainActivity</code> on bottom. When the user leaves the app and does other stuff, Android might decide to terminate the app for RAM/CPU usage reasons. When the user then reopens the app, <code>ActivityB</code> will restart and show up, but <code>MainActivity</code> will remain terminated (see <a href="https://developer.android.com/guide/components/tasks-and-back-stack.html" rel="nofollow noreferrer">documentation</a> under Saving Activity State). This is annoying, because the <code>AccountManager</code> instance will not be remade either, until the user decides to return to <code>MainActivity</code> - causing the functionality of <code>ActivityB</code> to be broken.</p>
  
  <p>Unfortunately, I can't use any <code>Context</code> instance for <code>AccountManager.get(Context)</code>, because I need to use the function <code>AccountManager.addOnAccountsUpdatedListener</code> which creates a need for the specified <code>Context</code> instance to remain instantiated, as also described in the <a href="https://developer.android.com/reference/android/accounts/AccountManager.html#get(android.content.Context)" rel="nofollow noreferrer">documentation</a>.</p>
  
  <p>Is there a nice way to go about this? Can I get an instance of <code>AccountManager</code> without having to rely on <code>MainActivity</code> being instantiated, possibly using a separate static class / singleton?</p>