Ziv Kesten
Ziv Kesten

Reputation: 1242

communicating between fragments when only one of them is visible

I have a problem with comuunicating between fragments that are chnged dynamicaly, i mannaged to use the interface system to cimmunicate if the fragments are all viewble but when i am viewing them sepretly i can't use a their actions since they dont have a tag yet, any thoughts?

main activty

public class MainActivity extends FragmentActivity implements OnClickListener, Communicator {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

if (isSingleFragment()) {

        Button btn_color = (Button) findViewById(R.id.button_color);
        Button btn_text = (Button) findViewById(R.id.button_text);
        Button btn_list = (Button) findViewById(R.id.button_list);
        btn_color.setOnClickListener(this);
        btn_list.setOnClickListener(this);
        btn_text.setOnClickListener(this);

        singleFragActions();

    } else {

        doubleFragActions();
    }


// some more stuff happens

@Override
public void apply(String data) {

    Log.d("data", data+"");
    FragmentManager fm = getSupportFragmentManager();
    Frag_list frag_list = (Frag_list) fm.findFragmentByTag("list");
//i cannot use id because the fragment in 
//MainActivity are in a layout and are enterd dynamicaly

    frag_list.addItem(data);

}

three fragments here, when on small screen, on is shown and three button on the top change them arrounf, if on tablet, all three are displyed, this is the interface

public interface Communicator {

public void apply(String data);

}

frag_text (first fragment)

public class Frag_text extends Fragment implements OnClickListener{


Communicator comm;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // stuff
return view;

}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

    comm = (Communicator) getActivity();
}
@Override
public void onClick(View v) {


    EditText input = (EditText) getView().findViewById(R.id.frag_text_edit);
    TextView output = (TextView) getView().findViewById(R.id.frag_text_show);

    String text_get = input.getText().toString();
    output.setText("hello "+text_get+" how are you today?");

    comm.apply(text_get);
}
}

frag_list (other fragment)

public class Frag_list extends Fragment{



ArrayList<String> list;

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

list = new ArrayList<String>();



ArrayAdapter<String> adapter = 
    new ArrayAdapter<String>(getActivity(), 
         android.R.layout.simple_list_item_1, list);

ListView lv = (ListView) view.findViewById(R.id.list_frag);
lv.setAdapter(adapter);

return view;

}

public void addItem(String data){

    list.add(data);
}
}

how to i access the other fragment if it doesnt have a tag yet?

Upvotes: 0

Views: 69

Answers (1)

Toguard
Toguard

Reputation: 447

If you happen to know what fragment you will be calling then FragmentActivity could act as a mediator in this case, as it is a mere "host". You already have this Communicator interface. Have it communicate to a given fragment:

public interface Communicator {
    public void apply(String data, String tag);
}

And the override:

@Override
public void apply(String data, String tag) {
    FragmentManager fm = getSupportFragmentManager();
    Frag_list frag_list = (Frag_list) fm.findFragmentByTag(tag);
    //Fallback plan in case the fragment has not been created.
    if(frag_list == null) {
        frag_list = new Frag_list();
        //Include the fragment for future reference.
        //This fragment has no view
        fm.beginTransaction().add(frag_list, tag);
    }
    frag_list.addItem(data);
}

And modify the fragment to always have the list available:

public class Frag_list extends Fragment{

ArrayList<String> list = new ArrayList<String>();
//...

    public void addItem(String data){
        //Add check to avoid problems
        if(list != null) {
            list.add(data);
        }
    }
}

Upvotes: 1

Related Questions