2

Hi Iam using Event bus to pass data from one fragment to another Fragment

From fragment-1 I am doing as below

@Override
public void onPause() {
    bsValues = new BoreShaftValues(strtext, strtextshaft);
    bus.post(bsValues);
    super.onPause();
}

In Fragment-2 I registered bus in OnActivitycreated

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    bus.register(this);
}

Then I placed OnEvent() method in fragment-2

public void onEvent(BoreShaftValues event){
    boregradeselect.setText(event.getBoreData());
    shaftgradeselect.setText(event.getShaftData());
}

Below is my BoreshaftVales class

public class BoreShaftValues {
    private String boredata;
    private String shaftdata;

    public BoreShaftValues(String boredata, String shaftdata){          
        this.boredata = boredata;
        this.shaftdata = shaftdata;
    }

    public String getBoreData(){
        return boredata;
    }
    public String getShaftData(){
        return shaftdata;
    }
}

But this OnEvent() method is not getting called at all. Am i doing it the rightway?

Let'sRefactor
  • 3,303
  • 4
  • 27
  • 43
hari86
  • 659
  • 2
  • 16
  • 28
  • @sasikumar no errors. Unable to setText() to my Textviews – hari86 Feb 16 '16 at 09:49
  • event.getBoreData() get value? – sasikumar Feb 16 '16 at 09:51
  • @sasikumar Not able to check event.getBoreData() since OnEvent is not calling. I can see bus.post(bsValues); has values in it while posting – hari86 Feb 16 '16 at 09:55
  • bus ? is class? where is that code – sasikumar Feb 16 '16 at 09:57
  • @sasikumar include EventBus.jar to my buildpath and using as private EventBus bus = EventBus.getDefault(); – hari86 Feb 16 '16 at 09:59
  • @sasikumar In my activity i am replacing the fragment1 with fragment 2. And i want my data in fragment 1 to be reflected in fragment 2. so posting data in Onpause – hari86 Feb 16 '16 at 10:05
  • @sasikumar First tried with onResume() it did not work then i moved to OnPause() – hari86 Feb 16 '16 at 10:09
  • @sasikumar when fragment-2 is created it should call for OnEvent() method right? .I placed a breakpoint on OnEvent().It is not going there. – hari86 Feb 16 '16 at 10:10
  • @sasikumar I Placed OnEvent() code in fragment-2 .Please refer my question for how i am posting from fragment 1 and getting in fragment 2 – hari86 Feb 16 '16 at 10:16
  • try this public void onEvent(){ BoreShaftValues event=new BoreShaftValues(); boregradeselect.setText(event.getBoreData()); shaftgradeselect.setText(event.getShaftData()); } – sasikumar Feb 16 '16 at 10:21
  • @sasikumar Throwing exception de.greenrobot.event.EventBusException: Subscriber class com.tss.isofitsupdate.fragments.ToleranceFragment has no public methods called onEvent – hari86 Feb 16 '16 at 10:29
  • sorry Unfortunately, this onEvent() method is never called...refer http://stackoverflow.com/questions/25474829/how-to-use-the-eventbus-onevent-method – sasikumar Feb 16 '16 at 10:32

1 Answers1

1

I typically try to tie EventBus back to the Activity and yet enable it to be loosely coupled. So in the Fragment lifecycle I register EventBus in the onAttach and unregister it in the onDetach methods in the fragment.

Peter Birdsall
  • 3,159
  • 5
  • 31
  • 48