0

Ive read several places (here, here or here) on how to do this and Ive tried to follow them, but im missing something that im not sure what is. I want to send data from FragmentAlarm to FragmentHome. The code below is my attempt on this. Im trying to mess around a bit.

1) Why am i not getting any outputs?

2) Also do i HAVE to go through MainActivity in order to communicate between 2 fragments? I've tried to use a public static method combined with FragmentStatePagerAdapter to "avoid" using MainActivity. but im not sure if that is possible or not.

Thanks in advance

FragmentAlarm

public class FragmentAlarm extends Fragment implements SeekBar.OnSeekBarChangeListener{

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

private String mParam1;
private String mParam2;

public FragmentAlarm() { // Required empty public constructor}

public static FragmentAlarm newInstance(String param1, String param2) {
    FragmentAlarm fragment = new FragmentAlarm();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    //put any extra arguments that you may want to supply to this fragment
    fragment.setArguments(args);
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.tab_alarm,container,false);

    FragmentAlarm.newInstance(ARG_PARAM1,"test from alarm");
    FragmentHome.newInstance(ARG_PARAM1,"test from alarm");

    return v;
}

 @Override
 public void onStart() {
    super.onStart();
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
        System.out.println("mParam1 " + mParam1);
        System.out.println("mParam2 " + mParam2);
    }

  }
}

FragmentHome

public class FragmentHome extends Fragment implements ValuesLoadedListener, SwipeRefreshLayout.OnRefreshListener {

private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;

public FragmentHome() {// Required empty public constructor }

// TODO: Rename and change types and number of parameters
public static FragmentHome newInstance(String param1, String param2) {
    FragmentHome fragment = new FragmentHome();
    Bundle args = new Bundle();
    //put any extra arguments that you may want to supply to this fragment
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onStart() {
    super.onStart();
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
        System.out.println("mParam1 " + mParam1);
        System.out.println("mParam2 " + mParam2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.tab_home,container,false);

    FragmentAlarm.newInstance(ARG_PARAM1,"test from home");
    FragmentHome.newInstance(ARG_PARAM1,"test from home");

    ...

    return v;
}

PagerAdapter

public class MyViewPagerAdapter extends FragmentStatePagerAdapter {


private Context context;
int icons[] = {R.drawable.ic_home,
        R.drawable.ic_graph,
        R.drawable.ic_bell_mid,
        R.drawable.ic_settings};

public MyViewPagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    this.context = context;
}

@Override
public Fragment getItem(int position) {
    if(position == 0) // if the position is 0 we are returning the First tab
    {
        return new FragmentHome();
    }
    else if (position == 1)
    {
        return new FragmentTrend();
    }
    else if (position == 2)
    {
        return new FragmentAlarm();
    }
    else
    {
        return new FragmentSetting();
    }
}

@Override
public int getCount() {
    return 4;
}

public Drawable getIcon(int position)
{
    return context.getResources().getDrawable(icons[position]);
}



}
Community
  • 1
  • 1
Celly
  • 325
  • 1
  • 14

2 Answers2

0

two ways I communicate between fragments.

1) If I know which fragments method/data to be called, I get the fragments instance and call it. You can get instance via fragmentManager's apis via tag.

2) If multiple fragments can respond to my method/data calling, I use BroadcastReceivers. In this way, register BR in each fragment you would like to listen call from one/more fragments. And sendBroadcast from specific fragment.

Ask for more detail if required?

AAnkit
  • 27,299
  • 12
  • 60
  • 71
  • yes, i kind of know that. i say kind of because ive tried an attempt, but it doesnt seem to work. could you please relate what you said with a simple sample code related to my code i posted? I still dont know whether or not i HAVE to use activity to communicate or not. Thanks :) @AAnkit – Celly Nov 17 '15 at 22:54
  • which option you want example of? if you are using BR, then you have to use same Intent Action for sending/receiving Broadcasts. – AAnkit Nov 17 '15 at 22:56
  • i would like to understand why my output is still null even though when i tried to pass a text to the fragment through new instance, please. Thanks for your help @AAnkit – Celly Nov 18 '15 at 13:12
  • Because you are not suppose to call newInstance. when one instance is already created, use the instance of fragment which is already created. – AAnkit Nov 18 '15 at 16:55
0

First, I want to say that I think it's probably not a good idea for two fragments to communicate directly. Part of the reason for using Fragments is so that they can be re-usable pieces. But having two of them tightly-coupled like that means that they can't be used independently.

Assuming that both Fragments are on the screen at the same time (hosted by the same parent Activity), the standard pattern is to create interfaces for both fragments and one for the Activity, and to have the the Fragments communicate through the Activity.

That said, it's also certainly possible to use a message bus.

Take a look at Square's Otto or greenrobot's EventBus.

The advantage of using a message bus is that, like using interfaces to communicate through an Activity, the bus would keep the Fragments from having to know about each other. They would both have knowledge of the Bus, but with some careful planning, you create an interface for the bus, and then wrap the actual message bus (Otto or EventBus) in a class that implements that interface.

Using a message bus is a lot easier than using a BroadcastReceiver, and because it isn't dependent on the Android sdk or libraries, you can easily mock / test it.

GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • so basically im using a direct communication? but then why am i not getting any output on system.out.print? the mparam 1 and 2 is always null. i dont understand why. @GreyBeardedGeek – Celly Nov 18 '15 at 13:03
  • system.out doesn't do anything useful on Android. Try using the built-in logging facilities. – GreyBeardedGeek Nov 18 '15 at 19:31