Sorry for the bad title, I wasn't sure how to express the situation, so I wrote it this way.
I recently asked a question about firebase I am unable to getValue() from a child in Firebase
I wanted to show a toast whenever the provided string matches a string in the firebase node. However, when I press the button after entering the text, it displays the toast message, which is what I wanted, but I don't want to navigate to the next fragment.
here is my code
nextBtn.setOnClickListener(view1 -> {
Query query = userOneRef.orderByChild("AmazonLink").equalTo(amazonLink.getText().toString());
query.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
for (DataSnapshot ds : task.getResult().getChildren()) {
String amazonLink = ds.child("AmazonLink").getValue(String.class);
Toast.makeText(getActivity(), "Amazon link already on tracker",
Toast.LENGTH_LONG).show();
Log.d("TAG", amazonLink);
Log.d("TAG","Testing1");
}
if (TextUtils.isEmpty(amazonLink.getText().toString())){
Toast.makeText(getActivity(), "Empty field not allowed!",
Toast.LENGTH_LONG).show();
Log.d("TAG","Testing2");
}
else {
Log.d("TAG","Testing3");
Bundle amazonLinkBundle = new Bundle();
amazonLinkBundle.putString("passingLink",amazonLink.getText().toString());
getParentFragmentManager().setFragmentResult("gettingLink",amazonLinkBundle );
Fragment fragment = new FinalHome_Fragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.addToBackStack(String.valueOf(Home_Fragment.class));
fragmentTransaction.commit();
}
} else {
Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
}
});
});
CodePudding user response:
Considering this is the part you are referring to:
for (DataSnapshot ds : task.getResult().getChildren()) {
String amazonLink = ds.child("AmazonLink").getValue(String.class);
Toast.makeText(getActivity(), "Amazon link already on tracker",
Toast.LENGTH_LONG).show();
Log.d("TAG", amazonLink);
Log.d("TAG","Testing1");
}
I'd change it to:
boolean isFound = false;
for (DataSnapshot ds : task.getResult().getChildren()) {
String amazonLink = ds.child("AmazonLink").getValue(String.class);
Toast.makeText(getActivity(), "Amazon link already on tracker",
Toast.LENGTH_LONG).show();
Log.d("TAG", amazonLink);
Log.d("TAG","Testing1");
isFound = true;
}
if(isFound)
return;
something of the sort of this, if that is what you were missing