0

I have two firebase project in my android application which I set in Application class all works properly. I call addValueEventListener on fragment start and I also call removeEventListener on fragment destroy but, my listener not stop any changes on db it will call itself. Same listener object I removing, andaddValueEventListener not call from anywhere.

Here is my application class

public class MyApplication extends Application {
private static MyApplication myApplication;
private SharedPreferenceManager sharedPreferenceManager;
private DatabaseReference reference;

public static MyApplication getMyApplication() {
    return myApplication;
}

@Override
public void onCreate() {
    super.onCreate();
    myApplication = this;

    FirebaseOptions options = new FirebaseOptions.Builder()
            .setApplicationId(getString(R.string.firebaseApplicationId))
            .setApiKey(getString(R.string.firebaseApiKey))
            .setDatabaseUrl(getString(R.string.firebaseDatabaseUrl))
            .setGcmSenderId(getString(R.string.firebaseGCMDefaultSenderId))
            .setProjectId(getString(R.string.firebaseProjectId))
            .setGaTrackingId(getString(R.string.firebaseCrashReporting))
            .setStorageBucket(getString(R.string.firebaseStorageBucket))
            .build();

    FirebaseApp currentApp = FirebaseApp.initializeApp(this, options, "secondary");
    FirebaseDatabase.getInstance(currentApp).setPersistenceEnabled(true);
    reference = FirebaseDatabase.getInstance(currentApp).getReference().child("schedules");
    reference.keepSynced(true);
}

public DatabaseReference getFirebaseReference() {
    return reference;
}
} 

Here is my calling function

public void getData(){
    final Query query = MyApplication.getMyApplication().getFirebaseReference().child(userId).child(year).child(month);
    listener = query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            Log.d("TAG", "onDataChange: "+ listener);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

@Override
public void onDestroy() {
    view = null;
    if (listener != null) {
        Log.d("TAG", "onDestroy: "+ listener);
MyApplication.getMyApplication().getFirebaseReference().removeEventListener(listener);
    }
}

removeEventListener have same listener object which I assign from addValueEventListener listener object. Here is my code please help me

TAG: onDataChange: Presenter$2@e4afd4f
TAG: onDestroy: Presenter$2@e4afd4f
TAG: onDataChange: Presenter$2@e4afd4f
  • Check **[this](https://stackoverflow.com/questions/48861350/should-i-actually-remove-the-valueeventlistener/48862873)** out. – Alex Mamo Mar 13 '20 at 14:02
  • I am removing according to the life-cycle; getData() call onCreateView and removeEventListener call onDestroyView – Mohammad Sultan Al Nahiyan Mar 13 '20 at 14:16
  • I'm not sure I understand the behavior you're seeing. Can you add some `Log.d` statements to the code that highlight the problem, and then edit your question to show the updated code and its output? – Frank van Puffelen Mar 13 '20 at 14:18
  • As I see you are removing the listener twice, why? Try to use in `onStart` and `onStop`. – Alex Mamo Mar 13 '20 at 14:18
  • can you try this instead MyApplication.getMyApplication().getFirebaseReference().child(userId).child(year).child(month).removeEventListener(listener); or even better if you can save the query object also and remove listener directly from it. – Mustafa Hussain Mar 13 '20 at 14:47

1 Answers1

0
DatabaseReference databaseReference = MyApplication.getMyApplication().getFirebaseReference().child(userId).child(year).child(month);
listener = databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });




@Override
public void onDestroy() {
    if (listener != null && databaseReference != null) {
        databaseReference.removeEventListener(listener);
    }
}