I currently have a problem with my Firebase Firestore query after closing the session, it happens that when I close the session I reload the activity (I am working with Fragments in Android Studio) when it arrives I get the Toast that I have put for when there is an error in the query, however when I close the application and reopen it without an active session, it loads normally...
This code is what I am using to close the session from the Activity
FirebaseAuth mAuth = FirebaseAuth.getInstance();
MenuItem itemCloseSession = binding.navView.getMenu().findItem(R.id.nav_SigOut);
itemCloseSession.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
mAuth.signOut();
finish();
overridePendingTransition(0,0);
startActivity(getIntent());
overridePendingTransition(0,0);
return false;
}
});
This is the code that I have In the Fragment to perform the query and fill the ArrayList that will work with the adapter and then display the data in a recycler view inside the fragment.
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot();
return root;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ArrayList<Product> productArrayList = new ArrayList<>();
ProductAdapter productAdapter = new ProductAdapter(getActivity(), getContext(), productArrayList, firestore);
binding.rvListProducts.setHasFixedSize(true);
binding.rvListProducts.setLayoutManager(new LinearLayoutManager(getContext()));
binding.rvListProducts.setAdapter(productAdapter);
getProducts();
}
public void getProducts(){
firestore.collection("products")
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot value, @Nullable FirebaseFirestoreException error) {
if(error != null){
Toast.makeText(getActivity().getApplicationContext(), "Failed to retrieve data", Toast.LENGTH_SHORT).show();
return;
}
for (DocumentChange dc : value.getDocumentChanges()){
if(dc.getType() == DocumentChange.Type.ADDED){
productArrayList.add(dc.getDocument().toObject(Product.class));
}
productAdapter.notifyDataSetChanged();
}
}
});
}
I am starting in this programming and although I know that there are thousands of ways to do what I am doing there, I would like to know which is the best way to do this process and that I do not get an error when closing the session and reloading the activity.
In context, I have seen examples where they have the Login in a different activity, therefore when they log out they go to the login activity and then put the security parameter so that they cannot return to the previous activity without first logging in... In In my case, I only have 1 activity and with it I have all the other fragments of my application, I am using Navigation Drawer for navigation, what I would like to achieve is to close the session without going to another activity, that is why I am trying to do finish()
a current activity and I reload the same one, but that's where I get the error in the query of the products to firebase.
Thanks in advance for your comments, I'm on the lookout.
I have found the error, below I leave my answer for clarity of the people who may be able to serve them...
In my particular case, it happened that when creating the database in the Firebase Firestore, in the read and write rules I had declared the following line of code.
What this allowed me to do is READ or make changes to the database as long as there was an active user session, honestly I had forgotten this configuration, I came to notice it when I converted the error to a text string to display it in a Toast, the query threw me an error, so I did
I changed my text that actually did not show me the error for the message that Firebase was throwing at me, I noticed that it was a permissions issue, when I closed the session, it automatically left me without permissions to read in the database, for this, I used a time conditional directly in Firestore...
Specifying a date that the application could have in development and use the database as a test.
I've done the tests and now the app works without any issues.