Firebase onDisconnect not called
<p>I was trying to cut down some active connection on my app. So I wanted to get disconnected from the <code>firebase</code> database when app is in the background. </p>

<p>So my <code>MainActivity</code> is the launcher activity. All other activities started from this activity are started on top of it. And <code>MainActivity</code> never get destroyed when a new activity is launched. So, I put a very simple logic to connect and disconnect from the <code>Firebase Database</code> like shown below:</p>

<p>In my Application's onCreate:</p>

<pre><code>@Override
public void onCreate() {
    ...
    // Check for the connection state
    final DatabaseReference presenceRef = FirebaseDatabase.getInstance().getReference("testing");
    // Write a string when this client loses connection
    presenceRef.onDisconnect().setValue("Disconnected");
    FirebaseDatabase.getInstance().getReference(".info/connected").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.getValue(Boolean.class)) {
                Log.v(TAG, "I am just connected");
                presenceRef.setValue("Connected");
            }
            else {
                Log.v(TAG, "I am just disconnected");
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Log.v(TAG, "database connection error: " + databaseError.getMessage());
        }
    });
    ..
}
</code></pre>

<p>And my connection and disconnection logic is set in <code>MainActivity</code> like this:</p>

<pre><code>protected void onCreate(Bundle savedInstanceState) {
    ...
    FirebaseDatabase.getInstance().goOnline();
    ...
}

@Override
protected void onDestroy() {
    FirebaseDatabase.getInstance().goOffline();
    Toast.makeText(mContext, "Disconnecting", Toast.LENGTH_SHORT).show();
    super.onDestroy();
}
</code></pre>

<p>If press back button on <code>MainActivity</code>, db get disconnected. But, if I press home button, it is not disconnected that I understand as <code>onDestroy</code> will not be called.</p>

<p>But when I go to recent and swipe away my app, I can still see "Connected" at the testing location. I read somewhere that the <code>onDisconnect</code> is registered at the server end. So, if server sees a client disconnected, it should call <code>onDisconnected</code> function.</p>

<p>Isn't this unexpected behavior?</p>