0

here we go again. I am writing an app using fragments. Stefan de Bruijn suggested this would be better than using the deprecated TabHost and he was right, Thank you Stefan.

I have finally got communication from one fragment to me Activity working thanks to help from other members (You know who you are, Thank you all).

I now have what hopefully is one last problem. My app has TextBox at the top that is part of the Activity, a permanent ListFragment on the left and FrameLayout on the right, to allow different Fragments to be displayed.

Is there any way of creating a generic "listener" if you like in the Activity that all the different Fragments can talk to?

To get one Fragment passing data I have used the following.

MainActivity

import com.example.fragger.CoreFragment.OnDataPass;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;

public class MainActivity extends Activity implements OnDataPass {

and Fragment Code:-

package com.example.fragger;


import android.app.Activity;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.view.View.OnClickListener;


public class CoreFragment extends Fragment{

int index;
Button Button1,Button2,Button3;
String Str,data;
OnDataPass dataPasser;

@Override
public void onAttach(Activity a) {
    super.onAttach(a);
        dataPasser = (OnDataPass) a;
}


public static CoreFragment newInstance(int index) {
    CoreFragment coreFragment = new CoreFragment();
    coreFragment.index = index;
    return coreFragment;
}


public interface OnDataPass {
    public void onDataPass(String data);

}

Which is all well and good until I show a different Fragment in my Frame (e.g. PlaceFragment). As the onDataPass is imported from CoreFragment and implemented, I cannot use it with anything else.

Is there any way around this?

Thanks all in advance. Gary

Gary
  • 69
  • 1
  • 9

1 Answers1

2

For communication between fragments you can use an EventBus. The EventBus makes your activity and fragments loosly coupled.

The first step is defining an EventType. For example: CarSelectedEvent

Upon selection of a Car (or some text type in your case) the CarSelectedEvent must be posted on the EventBus. Example:

eventBus.post(new CarSelectedEvent("volvo"));

All fragments or activities interested in the Event have to implement a method called:

onEvent(CarSelectedEvent event){
... update your view
}

Assume your have 3 fragments showing car details, each fragment receives the CarSelectedEvent and can update the view. When removing a fragment from the screen (for example on a smaller screen or a screen rotation) the logic does not change. The only difference is less fragments receiving the event.

You can find more information about EventBus on https://github.com/greenrobot/EventBus.

userM1433372
  • 5,345
  • 35
  • 38
  • Thanks for that. Have had a look at the EventBus on github and downloaded the zip file. Silly question probably but I am still learning in the android Dev world, how do I "bolt in" the eventbus to my app in eclipse? Sorry if I am asking a dumb question. – Gary Jan 24 '13 at 16:28
  • 1
    Put the jar in the libs folder of your project and add a jar dependency in Eclipse. On http://www.slideshare.net/greenrobot/eventbus-for-android-15314813 you can find a presentation about EventBus with examples. – userM1433372 Jan 25 '13 at 08:24
  • Okay, thanks for that, I will give it a try over the weekend. Have a good one all !! – Gary Jan 25 '13 at 16:44