How to start a new Intent after finishing Querysna

2019-08-28 01:50发布

I have used a OnSuccessListener to fetch data from firestore database. I want to start a new Intent after Fetching Data Task has finished it's execution. But the intent will be started before finishing the Task. Is there a way to fix this problem?

db.collection("users/user/driver").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
    @Override
    public void onSuccess(QuerySnapshot queryDocumentSnapshots1) {
        if (!queryDocumentSnapshots1.isEmpty()) {
            List<DocumentSnapshot> list1 = queryDocumentSnapshots1.getDocuments();
            for (final DocumentSnapshot d1 : list1) {
                String driverId = d1.getId();
                db.collection("users/user/driver").document(driverId).collection("checkpoints").get()
                        .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                            @Override
                            public void onSuccess(QuerySnapshot queryDocumentSnapshots2) {
                                // Some Code
                            }

                        }).addOnFailureListener(new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {

                            }
                });
            }
        }
        dialog.dismiss();
    }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
        }
    }
);


Intent intent = new Intent(PassengerRouteSearchActivity.this,PassengerRouteSearch2Activity.class);
intent.putStringArrayListExtra("filteredRoutes",filteredRoutes);
startActivity(intent);

1条回答
Anthone
2楼-- · 2019-08-28 02:30

You have to pass the intent in the inner onSuccess. Then it will work as you expected..

db.collection("users/user/driver").get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
        @Override
        public void onSuccess(QuerySnapshot queryDocumentSnapshots1) {
            if (!queryDocumentSnapshots1.isEmpty()) {
                List<DocumentSnapshot> list1 = queryDocumentSnapshots1.getDocuments();
                for (final DocumentSnapshot d1 : list1) {
                    String driverId = d1.getId();
                    db.collection("users/user/driver").document(driverId).collection("checkpoints").get()
                            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                                @Override
                                public void onSuccess(QuerySnapshot queryDocumentSnapshots2) {
                                    // Your Code.
                                }

                            }).addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {

                                }
                    });
                }
                Intent intent = new Intent(PassengerRouteSearchActivity.this,PassengerRouteSearch2Activity.class);
                intent.putStringArrayListExtra("filteredRoutes",filteredRoutes);
                startActivity(intent);
            }
            dialog.dismiss();
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
        }
});
查看更多
登录 后发表回答