简体   繁体   中英

Pass FirebaseFirestore reference in Intents

I need to send a FirebaseFirestore reference of a collection in intents from one activity to another which will be used to add documents by the second activity. How can I do that?

Pass the path of the document as a string as an extra in the intent. You can use getPath() to get the path from a DocumentReference, then use that same string to build a new DocumentReference by passing it to the document() method of FirebaseFirestore.

Try using getPath() and then passing it to document reference.

Example:

Getting path from the reference

String documentId = getSnapshots().getSnapshot(position).getReference().getPath();

                // Bring users to View Event when clicking on viewEventButton
                holder.viewEventButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view1) {
                        Intent intent = new Intent(getActivity(), ViewEventActivity.class);
                        intent.putExtra("documentId",documentId);
                        ((MainPageActivity) getActivity()).startActivity(intent);
                    }
                });

Getting Document from String path

        String documentId = getIntent().getStringExtra("documentId");
        String documentName = documentId.substring(documentId.lastIndexOf("/") + 1);

        // TODO how to reflect document path based on the event that users click on the app -> documentPath -> SharedPreferences from HomePage
        DocumentReference docRef = db.collection("Events").document(documentName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM