0

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.

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) {

            }

        });
        

    }
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

1 Answers1

0

As I see in your code, you are using in the first part addValueEventListener(), which means that you are listening for real-time updates. That kind of listener should be removed according to the life cycle of your activity. Please also note, that the onDestroy is not always called.

However, when using addListenerForSingleValueEvent(), there is no need to remove any listener, because it only reads the data once.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193