سؤال

I am trying to pass a value from one fragment to another. But I am always getting the values as null in the other fragment. My codes are as below.Please tell me what I am doing wrong.

senttask.java

          String p1=tname.getText().toString();
          Fragment fr;
          fr=new TaskSentFragment();
          FragmentManager fm=getFragmentManager();
          android.app.FragmentTransaction ft=fm.beginTransaction();
          Bundle args = new Bundle();
          args.putString("CID ", p1);
          Log.i("cid",""+p1);

          fr.setArguments(args);
          ft.replace(R.id.content_frame, fr);

                   ft.commit();

taskSentFragment.java

      String id=getArguments().getString("CID");
هل كانت مفيدة؟

المحلول

args.putString("CID ", p1); key is CID

This

String id=getArguments().getString("p1");

Should be

String id=getArguments().getString("CID");

Edit :

I am trying to pass a value from one fragment to another

I din't notice its Fragment to Fragment Communication.

You need to use interface as a call back to the activity. Then communicate value to other fragment

http://developer.android.com/training/basics/fragments/communicating.html

Quoting docs

Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top