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();
}
});
}