setPersistenceEnabled(true) crashes app
<p>I’m creating my first Firebase App. One of its requirements is that it run when the network is not available. The Firebase guide states: </p>

<blockquote>
  <p>Enabling disk persistence allows our app to also keep all of its state even after an app restart. We can enable disk persistence with just one line of code.
  <strong>FirebaseDatabase.getInstance().setPersistenceEnabled(true);</strong>
  With disk persistence enabled, our synced data and writes will be persisted to disk across app restarts and our app should work seamlessly in offline situations.</p>
</blockquote>

<p>Another requirement is to use Google Sign In. So in my <code>MainActivity</code> I check if the User is signed in, if not, I launch the <code>SignInActivity</code>. (The  <code>SignInActivity</code> is from the Firebase examples.)  The <code>SignInActivity</code> works, the user gets logged in, and <code>MainActivity</code> is launched for a second time.  Now my app crashes on the code line <code>FirebaseDatabase.getInstance().setPersistenceEnabled(true);</code>  with the following message:  </p>

<blockquote>
  <p>Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.</p>
</blockquote>

<p>Now if I restart my app, the User is signed in, the <code>SignInActivity</code> is not launched, my app runs fine.</p>

<p>Any suggestions of how I avoid this crash after the User signs in?</p>

<p>As I was posting this question, I received a suggestion to relocate <code>FirebaseDatabase.getInstance().setPersistenceEnabled(true);</code>
to my “Application class”. I get exactly the same result … <code>SignInActivity</code> starts, completes, and I get a crash on the <code>setPersistenceEnabled</code>.</p>

<p>Below is my MainActivity <code>onCreate</code>:</p>

<pre><code>@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Calls to setPersistenceEnabled() must be made before any other usage of FirebaseDatabase instance.
    // Crash here upon returning from SignInActivity.  
    FirebaseDatabase.getInstance().setPersistenceEnabled(true);
    mFirebaseDbReference = FirebaseDatabase.getInstance().getReference();

    // Initialize Firebase Auth
    mFirebaseAuth = FirebaseAuth.getInstance();
    mFirebaseUser = mFirebaseAuth.getCurrentUser();
    if (mFirebaseUser == null) {
        // Not signed in, launch the Sign In activity
        Timber.tag("MainActivity").i("onCreate(): User not signed in, launching SignInActivity");
        startActivity(new Intent(this, SignInActivity.class));
        finish();

    } else {
        mUsername = mFirebaseUser.getDisplayName();
        Timber.tag("MainActivity").i("onCreate(): User \"%s\" signed in.", mUsername);
        if (mFirebaseUser.getPhotoUrl() != null) {
            mPhotoUrl = mFirebaseUser.getPhotoUrl().toString();
        }
    } 
</code></pre>