-2

I want to send data from Teachers Fragment to OneTeacher Fragment. I found the following code from the internet. But it gives an output as "Null" when I clicked an item of the Listbox.

Please be kind enough to give a solution for this. (My question is why I can't send the data from Teachers Fragment to One Teacher Fragment by using following code).

Teachers Fragment

try {
      lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

               TextView myTextView = (TextView) view.findViewById(R.id.tName);

               String teachernamefrom = myTextView.getText().toString();

               Toast.makeText(getActivity(), teachernamefrom, Toast.LENGTH_SHORT).show();
               //this toast works properly

               Bundle bundle = new Bundle();
               bundle.putString("key1",teachernamefrom);

               getFragmentManager().beginTransaction()
                        .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
                        .replace(R.id.fragment_container, new OneTeacherFragment())
                        .addToBackStack("tag")
                        .commit();

               Fragment nextFrag = new OneTeacherFragment();
               nextFrag.setArguments(bundle);

            }
        });
    } catch (Exception e) {
        Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }

OneTeacherFragment:

        teacherName = (TextView) v.findViewById(R.id.teacher_name);
        Bundle bundle = getArguments();

        if (bundle != null){
            String key = bundle.getString("key1");
            teacherName.setText(key);
        }
        else{
            Toast.makeText(getActivity(), "Null", Toast.LENGTH_SHORT).show();
        }
  • 2
    Possible duplicate of [How to pass values between Fragments](https://stackoverflow.com/questions/16036572/how-to-pass-values-between-fragments) – Android Feb 25 '19 at 06:14
  • https://stackoverflow.com/questions/54763977/how-do-i-pass-a-custom-object-to-a-list-in-a-different-fragment/54764097 see this. – Salman Khan Feb 25 '19 at 06:14
  • **Fragment nextFrag = new OneTeacherFragment(); nextFrag.setArguments(bundle);** **getFragmentManager().beginTransaction() .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out) .replace(R.id.fragment_container, nextFrag) .addToBackStack("tag") .commit();** change your code like above code: – Mahesh Keshvala Feb 25 '19 at 06:15
  • Do efforts to do R&D before posting any questions – Suraj Bahadur Feb 25 '19 at 06:17

2 Answers2

0

change code in onItemClick

You are passing new object OneTeacherFragment() while replacing Fragment, You shoud pass nextFrag

// create OneTeacherFragment
Fragment nextFrag = new OneTeacherFragment();
// set bundle arguments
nextFrag.setArguments(bundle);

getFragmentManager().beginTransaction()
                    .setCustomAnimations(android.R.animator.fade_in, 
android.R.animator.fade_out)
                    .replace(R.id.fragment_container, nextFrag) // pass created nextFrag
                    .addToBackStack("tag")
                    .commit();
Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51
0

You have set the arguments and make transactions on that instance rather than the current approach

// Create an instance of the fragment
Fragment nextFrag = new OneTeacherFragment();

// Create an instance of a bundle
Bundle bundle = new Bundle();
// Pass data to the bundle
bundle.putString("key1",teachernamefrom);
// Set the arguments
nextFrag.setArguments(bundle);

// Perform the fragment transaction with the created instance
getFragmentManager().beginTransaction()
.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out)
.replace(R.id.fragment_container, nextFrag)
.addToBackStack("tag")
.commit();
Srikar Reddy
  • 3,650
  • 4
  • 36
  • 58