0

I am trying to get Firebase database but one fragment can get the data but the other fragment can't even call onDataChange. I was guessing if DB is opened in the other fragment maybe I can't open it again. It is just my guess.. if my guess is correct could you please let me know how to close it or how to fix this issue? The same code works in the other fragment and the other fragment is the first called fragment so I think DB structure has no issue.

This is not working code. I saw someone saying put the reading DB code in onViewCreated instead of onCreateView but still not working.

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_about, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        TextView textView = view.findViewById(R.id.txtSlogan);
        Button payNow = view.findViewById(R.id.btnPay);
        Myuser_pay = new UserModel();
        String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();

        FirebaseDatabase.getInstance().getReference().child("users").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Myuser_pay = dataSnapshot.getValue(UserModel.class);
                paiduser = Myuser_pay.paid;
                Toast.makeText(getActivity(),"OK",Toast.LENGTH_LONG);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(getActivity(),"Error",Toast.LENGTH_LONG);
            }
        });


        if (paiduser.equals("true")){
            textView.setText("Thank you for your payment. Your account is now fully activated");
        }

        payNow.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                processPayment();
            }
        });

    }
1
  • The other fragments are using recyclerview (used in Recycler adapter): recycleView.setAdapter(new PeopleFragmentRecyclerViewAdapter()); can this be the root cause?
    – David
    Commented Apr 14, 2020 at 3:58

1 Answer 1

0

Found answer after long debugging. It looks like this id due to my for loop which is using DB in recycler adapter. What I did was I created static class which can hold user data when user login and using that saved data instead of reading DB again. This simple fix could solve the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.