[英]How do I remove this firebase listener in OnDestroy to reduce memory leaks?
This is my product showing function.这是我的产品显示功能。 When I access this activity from another, the other activity gets destroyed maybe because of memory leaks.
当我从另一个活动访问此活动时,其他活动可能由于内存泄漏而被破坏。 How do I fix this issue?
我该如何解决这个问题? How do I close the listeners?
如何关闭监听器? I want to destroy this is OnDestroy method because it might be causing a memory leak and also I've read that it's a good practice to do so.
我想销毁这是 OnDestroy 方法,因为它可能会导致内存泄漏,而且我已经读到这样做是一个很好的做法。
private void getProductDetails(String productID) {
DatabaseReference productsRef = FirebaseDatabase.getInstance().getReference().child("Products");
productsRef.child(productID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NotNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
Products products = dataSnapshot.getValue(Products.class);
productName.setText(products.getPname());
productPrice.setText(products.getPrice());
if (products.getInStock().equals("Yes")){
inStock.setText("In Stock");
inStock.setTextColor(ContextCompat.getColor(ProductDetailsActivity.this,R.color.primary));
}else {
inStock.setText("Not In Stock");
inStock.setTextColor(ContextCompat.getColor(ProductDetailsActivity.this,R.color.red));
addToCartBtn.setEnabled(false);
}
ifFreeDelivery = "No";
DatabaseReference mref;
final String[] phone = new String[1];
mref = FirebaseDatabase.getInstance().getReference("Values").child("deliveryCharge");
mref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
phone[0] = dataSnapshot.getValue(String.class);
deliveryCharge = Integer.parseInt(phone[0]);
//do what you want with the likes
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
Products productsImg = dataSnapshot.getValue(Products.class);
ArrayList<String> test = productsImg.getImage();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference myRef = rootRef.child("Products").child(products.getPid()).child("image");
myRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
//this will be called for every child in your list
test.add(s);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
//this will be called for every child changed in your list
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
ArrayList<SlideModel> images = new ArrayList<>();
for(int i = 0; i < test.size() ; i++) {
images.add(new SlideModel(test.get(i),null));
}
productImage.setImageList(images);
productImgDownloadBtn.setOnClickListener(v -> {
for(int i = 0; i < products.getPicCount() ; i++) {
String url = test.get(i).replace(" ","");
DownloadImage(url);
}
});
productDescription.setText(products.getDescription());
productDetailsCopyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("Product description",products.getDescription());
clipboard.setPrimaryClip(clip);
Toast.makeText(ProductDetailsActivity.this, "Product description copied..", Toast.LENGTH_SHORT).show();
}
});
//Picasso.get().load(products.getImage().get(0)).into(productImage);
}
}
@Override
public void onCancelled(@NonNull @NotNull DatabaseError databaseError) {
}
});
}
As I see in your code, you are using in the first part addValueEventListener()
, which means that you are listening for real-time updates.正如我在您的代码中看到的,您在第一部分使用
addValueEventListener()
,这意味着您正在侦听实时更新。 That kind of listener should be removed according to the life cycle of your activity . 应该根据您的 Activity 的生命周期删除这种侦听器。 Please also note, that the onDestroy is not always called .
另请注意, 并不总是调用 onDestroy 。
However, when using addListenerForSingleValueEvent()
, there is no need to remove any listener, because it only reads the data once.但是,在使用
addListenerForSingleValueEvent()
,不需要删除任何侦听器,因为它只读取一次数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.