viewpager fragments not receiving otto events from activity

431 views Asked by At

I have an activity with a Viewpager. my viewpager has several fragments. i want to send Otto event to a fragment when it is selected, thus i implement ViewPager.OnPageChangeListener

     @Override
  public void onPageSelected(int position) {
     currentPosition = position;
    switch (position){
      case 0:
        EventBus.getInstance().post(new TypeEvent());
        break;
      case 1:
        EventBus.getInstance().post(new InternalEvent());
        break;
    }
  }

Inside my first fragment

@Override
  public void onStart() {
    super.onStart();
  EventBus.getInstance().register(this);
  }

  @Override
  public void onStop(){
    super.onStop();
    EventBus.getInstance().unregister(this);
  }

  @Subscribe
  public void init(TypeEvent event){
    Logger.d("type event received");
    //do something.......
  }

My event bus class

    public class EventBus extends Bus {

   private static EventBus eventBus;

    public static EventBus getInstance(){
        if(eventBus == null){
          eventBus = new EventBus();
        }
        return eventBus;
      }
    }

The problem is my fragments are not receiving events. what could be the problem?

2

There are 2 answers

0
Edijae Crusar On

The issue was that onPageSelected(int position) method wasn't being called even after setting adapter on my viewpager and caling viewPager.setCurrentItem(0); in my activity onCreate(Bundle savedInstanceState) method.

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pm);
    Toolbar toolbar =(Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    viewPager = (ViewPager)findViewById(R.id.pmViewpager);

    viewPager.addOnPageChangeListener(this);
    viewPager.setAdapter(new CustomAdapter(getSupportFragmentManager()));

    viewPager.setCurrentItem(currentPosition);
  }

i had to manually call onPageSelected(int position) through viewpager's post() method.

viewPager.post(new Runnable() {
          @Override
          public void run() {
            onPageSelected(viewPager.getCurrentItem());
          }
        });

Thus my overall onCreate(Bundle savedInstanceState) looked like this

@Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pm);
        Toolbar toolbar =(Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        viewPager = (ViewPager)findViewById(R.id.pmViewpager);

        viewPager.addOnPageChangeListener(this);
        viewPager.setAdapter(new CustomAdapter(getSupportFragmentManager()));

        viewPager.setCurrentItem(currentPosition);

viewPager.post(new Runnable() {
              @Override
              public void run() {
                onPageSelected(viewPager.getCurrentItem());
              }
            });
      }

All other pages(fragments) are receiving the events because onpageSelected() will be called when on scroll away from the first page. it's only the first page that wasn't receiving the event.

1
linjiang On

Maybe when the event comes, the eventbus hasn't been registed yet, especilly at the Fragment's first initialization time.

So u can let EventBus.getInstance().register(this); be called at onAttach() and unregister at onDettach.