0

I have two fragments. The first one get data from firebase, and the second one use the data. I am setting the data bundle using the setArguments() in first fragment and consuming it using getArguments in the second fragment. But i get a null value when consuming it. The below are the fragments.

FirstFragment;

firebaseFirestore.collection("QuizList")
                .document(quizId).collection("Results")
                .document(currentUserId).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                if(task.isSuccessful()){
                    DocumentSnapshot result = task.getResult();

                    Long correct = result.getLong("correct");
                    Long wrong = result.getLong("wrong");
                    Long missed = result.getLong("unanswered");

                    resultCorrect.setText(correct.toString());
                    resultWrong.setText(wrong.toString());
                    resultMissed.setText(missed.toString());

                    //Calculate Progress
                    Long total = correct + wrong + missed;
                    Long percent = (correct*100)/total;

                    ListFragment listFragment = new ListFragment();
                    Bundle bundle = new Bundle();
                    bundle.putLong("total", total);
                    bundle.putLong("percent", percent);
                    listFragment.setArguments(bundle);

                    resultPercent.setText(percent + "%");
                    resultProgress.setProgress(percent.intValue());
                }
            }
        }); 

SecondFragment:

@Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

Bundle bundle = this.getArguments();
        if (bundle != null) {
        Long total = getArguments().getLong("total", 0);
        Long percent = getArguments().getLong("percent",0);


        Toast.makeText(getActivity(),String.valueOf(total),Toast.LENGTH_SHORT).show();

        }
}

any help to do that? thank you!

Soundararajan
  • 2,000
  • 21
  • 23
Geekr
  • 13
  • 1
  • 6
  • Where's the code to show SecondFragment from the FirstFragment? I only saw you call `setArguments` of the SecondFragment without adding it to the `FragmentManager` – cartesify May 02 '20 at 04:40
  • @user7041125 i don't want to show SecondFragment from the FirstFragment, i just want to use this values from it in the SecondFragment, Without bringing it from Firebase again. – Geekr May 02 '20 at 04:50
  • That's not how it works. The values that you passed to `setArguments(...)` will only be available once you add it to the `FragmentManager`. To solve this, you can create 1 intermediary class which get the data from firebase. Once you received the callback, you can store it in a member variable of that intermediary class. Later on, both fragments can refer to the same data from that class. – cartesify May 02 '20 at 04:55
  • Thank you @user7041125, i'll try that. – Geekr May 02 '20 at 04:58
  • 1
    You can use this : https://stackoverflow.com/questions/56826346/kotiln-pass-data-from-adapter-to-activity/56827414#56827414 for working with `callback`. – milad salimi May 02 '20 at 05:52

0 Answers0