0

In my first fragment I have a listener which listens for a click and when that click occurs I would like to open up another fragment and pass data to it. I would like to know how I can pass data between two fragments here is what I have so far.

This is what I have in the first fragment

classificationGroup = group;
      aq.id(R.id.tv_group).text(LogGroup.GROUP_NAMES[group]);
      aq.id(R.id.tv_total).text("" + usage + " calls");

      Intent intent = new Intent(getActivity(), CallLogsFragment.class);
      Bundle b = new Bundle();
      b.putInt("key",classificationGroup); 
      intent.putExtras(b); 
      startActivity(intent);

This is what I have in second fragment

**Bundle b = getIntent().getExtras();
      if(b != null)
         value = b.getInt("key");
      if (getIntent().getIntExtra("Key", 0) != 0) {

      }**
mach
  • 8,315
  • 3
  • 33
  • 51
Zidane
  • 1,696
  • 3
  • 21
  • 35
  • possible duplicate of http://stackoverflow.com/questions/5194548/how-to-pass-data-between-fragments – ichthyocentaurs Oct 26 '16 at 08:29
  • @Ichthyocentaurs this cant be a duplicate because I mentioned that another fragment opens up opon click therefore I included intents in my questions as well – Zidane Oct 26 '16 at 08:33

3 Answers3

0

First create new Listener Interface:

public interface YourCustomListener {
    void onItemClick(int position, int ID);
}

Now add this to your first fragment:

YourCustomListener mListener;

And your fragment onclick method:

mListener.onItemClick(position,"YOUR ID");

Add this methods in your first fragment :

@Override
    public void onAttach(Context con) {
        super.onAttach(con);
        try {
            mListener= (YourCustomListener ) con;
        } catch (ClassCastException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDetach() {
        mListener=null;
        super.onDetach();
    }

Implement interface in Activity class. In derived method:

@Override
    public void onItemClick(int position,int ID) {
        fragment2 =new SecondFragment();
        Bundle arg= new Bundle();
        arg.putInt("ID",ID);
        arg.putString("title","ANY TITLE");
        fragment2.setArguments(arg);

        if (fragment2 != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.setCustomAnimations(R.anim.slide_left_enter,R.anim.slide_left_exit,R.anim.slide_left_enter,R.anim.slide_left_exit);
            fragmentTransaction.replace(R.id.container_body, fragment2,"PROF");
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
            getSupportActionBar().setLogo(null);
        }
    }

Now on Your Second Fragment, In on Create Method use this:

Bundle pb=getArguments();
        h=pb.getInt("ID");
        title=pb.getString("title");

Now you have Value of int h, and String Title.You can pass data as you need.

Divyesh Patel
  • 2,576
  • 2
  • 15
  • 30
0

In fragment_1

Bundle i = new Bundle(); 
            i.putString("name", "Emmanuel");

            Fragment_1 frag = new Fragment_1();
            frag.setArguments(i);
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.content_frame
                            , new Fragment_2())
                    .commit();

get the data in fragment_2

Intent intent = getActivity().getIntent();
    if (intent.getExtras() != null) {
        String name =intent.getStringExtra("name");
    }

Or you can do it using an Interface. Basic Communication between two fragments

Community
  • 1
  • 1
Adheesh
  • 83
  • 1
  • 11
0

Intents are used for activities, not for Fragments. So, you cannot start a fragment by calling "startActivity()".

You should use FragmentManager to manage your Fragments.

SecondFragment secondFragment = new SecondFragment(classificationGroup); getActivity().getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, secondFragment).commit();

Efe Budak
  • 659
  • 5
  • 27