How to cleanup data listeners/subscriptions from RecyclerView? [duplicate]











up vote
1
down vote

favorite













This question already has an answer here:




  • Should I actually remove the valueEventListener?

    2 answers




Note: Please do not suggest FirebaseUI as an answer.



I am trying to design a chat list UI. For this, I am populating data to my recyclerview which displays the list of chat groups along with last message. I want last message to be updated in real time. Therefore, to do this, I have some listeners/subscription inside onBindViewHolder method which continuously listen for new data and update the view.



The problem what I am facing is if the user migrates to some other activity, the app crashes when chat list data changes in the database. This is because the listeners are still running in background and trying to update views of a destroyed activity.



I am looking for a way to close my listeners/subscriptions when the recyclerview is destroyed. I have tried using onViewDetachedFromWindow but it only works for views that get recycled when the recycler is on screen. If i was reading data only once, i would have cleaned up subscriptions as soon as they complete but my use-case is to continuously listen for changes in data.



Some sample code:



protected void onBindViewHolder(@NonNull ChatViewHolder holder,
int position, @NonNull FirebaseConversationRecord model) {
final CardView cardView = (CardView) holder.itemView;
...
final ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.v("FIREBASEADAPTERLISTENER", dataSnapshot.getKey());
FirebaseUserRecord data = dataSnapshot.getValue(FirebaseUserRecord.class);
textViewName.setText(data.getName());

GlideApp.with(cardView.getContext())
.load(data.getProfilePicURL())
.into(imageView);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.v("FIREBASEADAPTERLISTENER", databaseError.getMessage());
}
};
...
}


EDIT
This question is in context of a RecyclerView and how to attach listeners during onBindView. It is not the same as adding/removing a single listener from an activity which is very straight forward to implement.










share|improve this question















marked as duplicate by Alex Mamo android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 8:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Please check the duplicate to see how you can remove the listener according to the life-cycle of your activity.
    – Alex Mamo
    Nov 22 at 8:50












  • If you consider at some point to try using Cloud Firestore, here you can find a tutorial on how to can create a functional Chat App.
    – Alex Mamo
    Nov 22 at 8:51










  • I am amazed how to see the duplicate linked question. I cannot understand how removing valueEventListener in an activity is same as removing it from recyclerview onBindView. By this logic you should mark all questions containing the keyword "valueeventlistener" and "remove" as duplicate. This is poor moderation according to me.
    – Adi
    Nov 22 at 15:09










  • It is the exact same thing. That's why I have marked it as a duplicate. Doesn't matter in which part of your acitivity you are adding the ValueEventListener, you should remove it according to the lyfe-cycle of your activity. Btw, using RecyclerView's onBindViewHolder method, it means that you are still in an activity and should be removed or, as in the duplicate, use a ListenerForSingleValueEvent, which adds a listener only once, right?
    – Alex Mamo
    Nov 22 at 16:01












  • I don't see it how is it same. Probably, it will be great and maybe beneficial to future noob users like me if you can answer the question with a code example.
    – Adi
    Nov 22 at 17:41















up vote
1
down vote

favorite













This question already has an answer here:




  • Should I actually remove the valueEventListener?

    2 answers




Note: Please do not suggest FirebaseUI as an answer.



I am trying to design a chat list UI. For this, I am populating data to my recyclerview which displays the list of chat groups along with last message. I want last message to be updated in real time. Therefore, to do this, I have some listeners/subscription inside onBindViewHolder method which continuously listen for new data and update the view.



The problem what I am facing is if the user migrates to some other activity, the app crashes when chat list data changes in the database. This is because the listeners are still running in background and trying to update views of a destroyed activity.



I am looking for a way to close my listeners/subscriptions when the recyclerview is destroyed. I have tried using onViewDetachedFromWindow but it only works for views that get recycled when the recycler is on screen. If i was reading data only once, i would have cleaned up subscriptions as soon as they complete but my use-case is to continuously listen for changes in data.



Some sample code:



protected void onBindViewHolder(@NonNull ChatViewHolder holder,
int position, @NonNull FirebaseConversationRecord model) {
final CardView cardView = (CardView) holder.itemView;
...
final ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.v("FIREBASEADAPTERLISTENER", dataSnapshot.getKey());
FirebaseUserRecord data = dataSnapshot.getValue(FirebaseUserRecord.class);
textViewName.setText(data.getName());

GlideApp.with(cardView.getContext())
.load(data.getProfilePicURL())
.into(imageView);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.v("FIREBASEADAPTERLISTENER", databaseError.getMessage());
}
};
...
}


EDIT
This question is in context of a RecyclerView and how to attach listeners during onBindView. It is not the same as adding/removing a single listener from an activity which is very straight forward to implement.










share|improve this question















marked as duplicate by Alex Mamo android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 8:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • Please check the duplicate to see how you can remove the listener according to the life-cycle of your activity.
    – Alex Mamo
    Nov 22 at 8:50












  • If you consider at some point to try using Cloud Firestore, here you can find a tutorial on how to can create a functional Chat App.
    – Alex Mamo
    Nov 22 at 8:51










  • I am amazed how to see the duplicate linked question. I cannot understand how removing valueEventListener in an activity is same as removing it from recyclerview onBindView. By this logic you should mark all questions containing the keyword "valueeventlistener" and "remove" as duplicate. This is poor moderation according to me.
    – Adi
    Nov 22 at 15:09










  • It is the exact same thing. That's why I have marked it as a duplicate. Doesn't matter in which part of your acitivity you are adding the ValueEventListener, you should remove it according to the lyfe-cycle of your activity. Btw, using RecyclerView's onBindViewHolder method, it means that you are still in an activity and should be removed or, as in the duplicate, use a ListenerForSingleValueEvent, which adds a listener only once, right?
    – Alex Mamo
    Nov 22 at 16:01












  • I don't see it how is it same. Probably, it will be great and maybe beneficial to future noob users like me if you can answer the question with a code example.
    – Adi
    Nov 22 at 17:41













up vote
1
down vote

favorite









up vote
1
down vote

favorite












This question already has an answer here:




  • Should I actually remove the valueEventListener?

    2 answers




Note: Please do not suggest FirebaseUI as an answer.



I am trying to design a chat list UI. For this, I am populating data to my recyclerview which displays the list of chat groups along with last message. I want last message to be updated in real time. Therefore, to do this, I have some listeners/subscription inside onBindViewHolder method which continuously listen for new data and update the view.



The problem what I am facing is if the user migrates to some other activity, the app crashes when chat list data changes in the database. This is because the listeners are still running in background and trying to update views of a destroyed activity.



I am looking for a way to close my listeners/subscriptions when the recyclerview is destroyed. I have tried using onViewDetachedFromWindow but it only works for views that get recycled when the recycler is on screen. If i was reading data only once, i would have cleaned up subscriptions as soon as they complete but my use-case is to continuously listen for changes in data.



Some sample code:



protected void onBindViewHolder(@NonNull ChatViewHolder holder,
int position, @NonNull FirebaseConversationRecord model) {
final CardView cardView = (CardView) holder.itemView;
...
final ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.v("FIREBASEADAPTERLISTENER", dataSnapshot.getKey());
FirebaseUserRecord data = dataSnapshot.getValue(FirebaseUserRecord.class);
textViewName.setText(data.getName());

GlideApp.with(cardView.getContext())
.load(data.getProfilePicURL())
.into(imageView);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.v("FIREBASEADAPTERLISTENER", databaseError.getMessage());
}
};
...
}


EDIT
This question is in context of a RecyclerView and how to attach listeners during onBindView. It is not the same as adding/removing a single listener from an activity which is very straight forward to implement.










share|improve this question
















This question already has an answer here:




  • Should I actually remove the valueEventListener?

    2 answers




Note: Please do not suggest FirebaseUI as an answer.



I am trying to design a chat list UI. For this, I am populating data to my recyclerview which displays the list of chat groups along with last message. I want last message to be updated in real time. Therefore, to do this, I have some listeners/subscription inside onBindViewHolder method which continuously listen for new data and update the view.



The problem what I am facing is if the user migrates to some other activity, the app crashes when chat list data changes in the database. This is because the listeners are still running in background and trying to update views of a destroyed activity.



I am looking for a way to close my listeners/subscriptions when the recyclerview is destroyed. I have tried using onViewDetachedFromWindow but it only works for views that get recycled when the recycler is on screen. If i was reading data only once, i would have cleaned up subscriptions as soon as they complete but my use-case is to continuously listen for changes in data.



Some sample code:



protected void onBindViewHolder(@NonNull ChatViewHolder holder,
int position, @NonNull FirebaseConversationRecord model) {
final CardView cardView = (CardView) holder.itemView;
...
final ValueEventListener listener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
Log.v("FIREBASEADAPTERLISTENER", dataSnapshot.getKey());
FirebaseUserRecord data = dataSnapshot.getValue(FirebaseUserRecord.class);
textViewName.setText(data.getName());

GlideApp.with(cardView.getContext())
.load(data.getProfilePicURL())
.into(imageView);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.v("FIREBASEADAPTERLISTENER", databaseError.getMessage());
}
};
...
}


EDIT
This question is in context of a RecyclerView and how to attach listeners during onBindView. It is not the same as adding/removing a single listener from an activity which is very straight forward to implement.





This question already has an answer here:




  • Should I actually remove the valueEventListener?

    2 answers








android firebase firebase-realtime-database android-recyclerview android-lifecycle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 15:11

























asked Nov 21 at 22:03









Adi

1,05421228




1,05421228




marked as duplicate by Alex Mamo android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 8:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Alex Mamo android
Users with the  android badge can single-handedly close android questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 8:50


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • Please check the duplicate to see how you can remove the listener according to the life-cycle of your activity.
    – Alex Mamo
    Nov 22 at 8:50












  • If you consider at some point to try using Cloud Firestore, here you can find a tutorial on how to can create a functional Chat App.
    – Alex Mamo
    Nov 22 at 8:51










  • I am amazed how to see the duplicate linked question. I cannot understand how removing valueEventListener in an activity is same as removing it from recyclerview onBindView. By this logic you should mark all questions containing the keyword "valueeventlistener" and "remove" as duplicate. This is poor moderation according to me.
    – Adi
    Nov 22 at 15:09










  • It is the exact same thing. That's why I have marked it as a duplicate. Doesn't matter in which part of your acitivity you are adding the ValueEventListener, you should remove it according to the lyfe-cycle of your activity. Btw, using RecyclerView's onBindViewHolder method, it means that you are still in an activity and should be removed or, as in the duplicate, use a ListenerForSingleValueEvent, which adds a listener only once, right?
    – Alex Mamo
    Nov 22 at 16:01












  • I don't see it how is it same. Probably, it will be great and maybe beneficial to future noob users like me if you can answer the question with a code example.
    – Adi
    Nov 22 at 17:41


















  • Please check the duplicate to see how you can remove the listener according to the life-cycle of your activity.
    – Alex Mamo
    Nov 22 at 8:50












  • If you consider at some point to try using Cloud Firestore, here you can find a tutorial on how to can create a functional Chat App.
    – Alex Mamo
    Nov 22 at 8:51










  • I am amazed how to see the duplicate linked question. I cannot understand how removing valueEventListener in an activity is same as removing it from recyclerview onBindView. By this logic you should mark all questions containing the keyword "valueeventlistener" and "remove" as duplicate. This is poor moderation according to me.
    – Adi
    Nov 22 at 15:09










  • It is the exact same thing. That's why I have marked it as a duplicate. Doesn't matter in which part of your acitivity you are adding the ValueEventListener, you should remove it according to the lyfe-cycle of your activity. Btw, using RecyclerView's onBindViewHolder method, it means that you are still in an activity and should be removed or, as in the duplicate, use a ListenerForSingleValueEvent, which adds a listener only once, right?
    – Alex Mamo
    Nov 22 at 16:01












  • I don't see it how is it same. Probably, it will be great and maybe beneficial to future noob users like me if you can answer the question with a code example.
    – Adi
    Nov 22 at 17:41
















Please check the duplicate to see how you can remove the listener according to the life-cycle of your activity.
– Alex Mamo
Nov 22 at 8:50






Please check the duplicate to see how you can remove the listener according to the life-cycle of your activity.
– Alex Mamo
Nov 22 at 8:50














If you consider at some point to try using Cloud Firestore, here you can find a tutorial on how to can create a functional Chat App.
– Alex Mamo
Nov 22 at 8:51




If you consider at some point to try using Cloud Firestore, here you can find a tutorial on how to can create a functional Chat App.
– Alex Mamo
Nov 22 at 8:51












I am amazed how to see the duplicate linked question. I cannot understand how removing valueEventListener in an activity is same as removing it from recyclerview onBindView. By this logic you should mark all questions containing the keyword "valueeventlistener" and "remove" as duplicate. This is poor moderation according to me.
– Adi
Nov 22 at 15:09




I am amazed how to see the duplicate linked question. I cannot understand how removing valueEventListener in an activity is same as removing it from recyclerview onBindView. By this logic you should mark all questions containing the keyword "valueeventlistener" and "remove" as duplicate. This is poor moderation according to me.
– Adi
Nov 22 at 15:09












It is the exact same thing. That's why I have marked it as a duplicate. Doesn't matter in which part of your acitivity you are adding the ValueEventListener, you should remove it according to the lyfe-cycle of your activity. Btw, using RecyclerView's onBindViewHolder method, it means that you are still in an activity and should be removed or, as in the duplicate, use a ListenerForSingleValueEvent, which adds a listener only once, right?
– Alex Mamo
Nov 22 at 16:01






It is the exact same thing. That's why I have marked it as a duplicate. Doesn't matter in which part of your acitivity you are adding the ValueEventListener, you should remove it according to the lyfe-cycle of your activity. Btw, using RecyclerView's onBindViewHolder method, it means that you are still in an activity and should be removed or, as in the duplicate, use a ListenerForSingleValueEvent, which adds a listener only once, right?
– Alex Mamo
Nov 22 at 16:01














I don't see it how is it same. Probably, it will be great and maybe beneficial to future noob users like me if you can answer the question with a code example.
– Adi
Nov 22 at 17:41




I don't see it how is it same. Probably, it will be great and maybe beneficial to future noob users like me if you can answer the question with a code example.
– Adi
Nov 22 at 17:41












2 Answers
2






active

oldest

votes

















up vote
0
down vote













remove ValueEvenetListener from DatabaseReference onDestroy using this following method



DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//here you gets the data
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
//if some error occurs
}
};

//add value event listener
databaseReference.addValueEventListener(valueEventListener);

//remove enet listener
databaseReference.removeEventListener(valueEventListener);


this is just a hint



ok do this way :



//here you require the instance of activity
//in your onDataCahnge method
if(activity.getApplicationContext() != null){
//implement all the ui change here
}else{
//here you remove the ValueEventListener
databaseReference.removeEventListener(valueEventListener);
}





share|improve this answer























  • This is a wrong answer. The question is in context of a RecyclerView and not an activity. There is one ValueEventListener bound to each viewHolder/item in the recyclerview. I have to clean all of it.
    – Adi
    Nov 21 at 22:20










  • you said it craches because the eventlistener still holding the destroyed views sp in that case activity life cycle is involved. ok leme give you more explaination how you can resolve the issue. wait a minute
    – Har Kal
    Nov 21 at 22:22










  • @Adi have a look at the update bro!
    – Har Kal
    Nov 21 at 22:44










  • The challenge with recycler view is that there will be one valueEventListener for each item in the recycler. I need a mechanism to consciously close down all of them. On way is that i keep a List<ValueEventListener> to track all the referenced recycler but i was hoping for a simpler way like onViewDetachedFromWindow. Your solution is only accounting for a very simple case. Please take complexities of recycler in account. It will be really helpful to me.
    – Adi
    Nov 22 at 15:14










  • the last approach i told u will work in your case too
    – Har Kal
    Nov 22 at 17:31


















up vote
0
down vote













Pass in or expose an instance of CompositeDisposable and clear your disposables in the onDestroy() of your Activity.






share|improve this answer




























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    remove ValueEvenetListener from DatabaseReference onDestroy using this following method



    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("");
    ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    //here you gets the data
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
    //if some error occurs
    }
    };

    //add value event listener
    databaseReference.addValueEventListener(valueEventListener);

    //remove enet listener
    databaseReference.removeEventListener(valueEventListener);


    this is just a hint



    ok do this way :



    //here you require the instance of activity
    //in your onDataCahnge method
    if(activity.getApplicationContext() != null){
    //implement all the ui change here
    }else{
    //here you remove the ValueEventListener
    databaseReference.removeEventListener(valueEventListener);
    }





    share|improve this answer























    • This is a wrong answer. The question is in context of a RecyclerView and not an activity. There is one ValueEventListener bound to each viewHolder/item in the recyclerview. I have to clean all of it.
      – Adi
      Nov 21 at 22:20










    • you said it craches because the eventlistener still holding the destroyed views sp in that case activity life cycle is involved. ok leme give you more explaination how you can resolve the issue. wait a minute
      – Har Kal
      Nov 21 at 22:22










    • @Adi have a look at the update bro!
      – Har Kal
      Nov 21 at 22:44










    • The challenge with recycler view is that there will be one valueEventListener for each item in the recycler. I need a mechanism to consciously close down all of them. On way is that i keep a List<ValueEventListener> to track all the referenced recycler but i was hoping for a simpler way like onViewDetachedFromWindow. Your solution is only accounting for a very simple case. Please take complexities of recycler in account. It will be really helpful to me.
      – Adi
      Nov 22 at 15:14










    • the last approach i told u will work in your case too
      – Har Kal
      Nov 22 at 17:31















    up vote
    0
    down vote













    remove ValueEvenetListener from DatabaseReference onDestroy using this following method



    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("");
    ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    //here you gets the data
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
    //if some error occurs
    }
    };

    //add value event listener
    databaseReference.addValueEventListener(valueEventListener);

    //remove enet listener
    databaseReference.removeEventListener(valueEventListener);


    this is just a hint



    ok do this way :



    //here you require the instance of activity
    //in your onDataCahnge method
    if(activity.getApplicationContext() != null){
    //implement all the ui change here
    }else{
    //here you remove the ValueEventListener
    databaseReference.removeEventListener(valueEventListener);
    }





    share|improve this answer























    • This is a wrong answer. The question is in context of a RecyclerView and not an activity. There is one ValueEventListener bound to each viewHolder/item in the recyclerview. I have to clean all of it.
      – Adi
      Nov 21 at 22:20










    • you said it craches because the eventlistener still holding the destroyed views sp in that case activity life cycle is involved. ok leme give you more explaination how you can resolve the issue. wait a minute
      – Har Kal
      Nov 21 at 22:22










    • @Adi have a look at the update bro!
      – Har Kal
      Nov 21 at 22:44










    • The challenge with recycler view is that there will be one valueEventListener for each item in the recycler. I need a mechanism to consciously close down all of them. On way is that i keep a List<ValueEventListener> to track all the referenced recycler but i was hoping for a simpler way like onViewDetachedFromWindow. Your solution is only accounting for a very simple case. Please take complexities of recycler in account. It will be really helpful to me.
      – Adi
      Nov 22 at 15:14










    • the last approach i told u will work in your case too
      – Har Kal
      Nov 22 at 17:31













    up vote
    0
    down vote










    up vote
    0
    down vote









    remove ValueEvenetListener from DatabaseReference onDestroy using this following method



    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("");
    ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    //here you gets the data
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
    //if some error occurs
    }
    };

    //add value event listener
    databaseReference.addValueEventListener(valueEventListener);

    //remove enet listener
    databaseReference.removeEventListener(valueEventListener);


    this is just a hint



    ok do this way :



    //here you require the instance of activity
    //in your onDataCahnge method
    if(activity.getApplicationContext() != null){
    //implement all the ui change here
    }else{
    //here you remove the ValueEventListener
    databaseReference.removeEventListener(valueEventListener);
    }





    share|improve this answer














    remove ValueEvenetListener from DatabaseReference onDestroy using this following method



    DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("");
    ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    //here you gets the data
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
    //if some error occurs
    }
    };

    //add value event listener
    databaseReference.addValueEventListener(valueEventListener);

    //remove enet listener
    databaseReference.removeEventListener(valueEventListener);


    this is just a hint



    ok do this way :



    //here you require the instance of activity
    //in your onDataCahnge method
    if(activity.getApplicationContext() != null){
    //implement all the ui change here
    }else{
    //here you remove the ValueEventListener
    databaseReference.removeEventListener(valueEventListener);
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 21 at 22:43

























    answered Nov 21 at 22:14









    Har Kal

    590314




    590314












    • This is a wrong answer. The question is in context of a RecyclerView and not an activity. There is one ValueEventListener bound to each viewHolder/item in the recyclerview. I have to clean all of it.
      – Adi
      Nov 21 at 22:20










    • you said it craches because the eventlistener still holding the destroyed views sp in that case activity life cycle is involved. ok leme give you more explaination how you can resolve the issue. wait a minute
      – Har Kal
      Nov 21 at 22:22










    • @Adi have a look at the update bro!
      – Har Kal
      Nov 21 at 22:44










    • The challenge with recycler view is that there will be one valueEventListener for each item in the recycler. I need a mechanism to consciously close down all of them. On way is that i keep a List<ValueEventListener> to track all the referenced recycler but i was hoping for a simpler way like onViewDetachedFromWindow. Your solution is only accounting for a very simple case. Please take complexities of recycler in account. It will be really helpful to me.
      – Adi
      Nov 22 at 15:14










    • the last approach i told u will work in your case too
      – Har Kal
      Nov 22 at 17:31


















    • This is a wrong answer. The question is in context of a RecyclerView and not an activity. There is one ValueEventListener bound to each viewHolder/item in the recyclerview. I have to clean all of it.
      – Adi
      Nov 21 at 22:20










    • you said it craches because the eventlistener still holding the destroyed views sp in that case activity life cycle is involved. ok leme give you more explaination how you can resolve the issue. wait a minute
      – Har Kal
      Nov 21 at 22:22










    • @Adi have a look at the update bro!
      – Har Kal
      Nov 21 at 22:44










    • The challenge with recycler view is that there will be one valueEventListener for each item in the recycler. I need a mechanism to consciously close down all of them. On way is that i keep a List<ValueEventListener> to track all the referenced recycler but i was hoping for a simpler way like onViewDetachedFromWindow. Your solution is only accounting for a very simple case. Please take complexities of recycler in account. It will be really helpful to me.
      – Adi
      Nov 22 at 15:14










    • the last approach i told u will work in your case too
      – Har Kal
      Nov 22 at 17:31
















    This is a wrong answer. The question is in context of a RecyclerView and not an activity. There is one ValueEventListener bound to each viewHolder/item in the recyclerview. I have to clean all of it.
    – Adi
    Nov 21 at 22:20




    This is a wrong answer. The question is in context of a RecyclerView and not an activity. There is one ValueEventListener bound to each viewHolder/item in the recyclerview. I have to clean all of it.
    – Adi
    Nov 21 at 22:20












    you said it craches because the eventlistener still holding the destroyed views sp in that case activity life cycle is involved. ok leme give you more explaination how you can resolve the issue. wait a minute
    – Har Kal
    Nov 21 at 22:22




    you said it craches because the eventlistener still holding the destroyed views sp in that case activity life cycle is involved. ok leme give you more explaination how you can resolve the issue. wait a minute
    – Har Kal
    Nov 21 at 22:22












    @Adi have a look at the update bro!
    – Har Kal
    Nov 21 at 22:44




    @Adi have a look at the update bro!
    – Har Kal
    Nov 21 at 22:44












    The challenge with recycler view is that there will be one valueEventListener for each item in the recycler. I need a mechanism to consciously close down all of them. On way is that i keep a List<ValueEventListener> to track all the referenced recycler but i was hoping for a simpler way like onViewDetachedFromWindow. Your solution is only accounting for a very simple case. Please take complexities of recycler in account. It will be really helpful to me.
    – Adi
    Nov 22 at 15:14




    The challenge with recycler view is that there will be one valueEventListener for each item in the recycler. I need a mechanism to consciously close down all of them. On way is that i keep a List<ValueEventListener> to track all the referenced recycler but i was hoping for a simpler way like onViewDetachedFromWindow. Your solution is only accounting for a very simple case. Please take complexities of recycler in account. It will be really helpful to me.
    – Adi
    Nov 22 at 15:14












    the last approach i told u will work in your case too
    – Har Kal
    Nov 22 at 17:31




    the last approach i told u will work in your case too
    – Har Kal
    Nov 22 at 17:31












    up vote
    0
    down vote













    Pass in or expose an instance of CompositeDisposable and clear your disposables in the onDestroy() of your Activity.






    share|improve this answer

























      up vote
      0
      down vote













      Pass in or expose an instance of CompositeDisposable and clear your disposables in the onDestroy() of your Activity.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        Pass in or expose an instance of CompositeDisposable and clear your disposables in the onDestroy() of your Activity.






        share|improve this answer












        Pass in or expose an instance of CompositeDisposable and clear your disposables in the onDestroy() of your Activity.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 3:58









        urgentx

        1,5231023




        1,5231023















            Popular posts from this blog

            Sphinx de Gizeh

            Dijon

            Guerrita