2

I am sending data from lets say SplashActivity to main activity using EventBus's postSticky method, and depending on that data am creating tabs in MainActivity using ViewPager.

So far it works great and I can setup ViewPager in @Subscribe onEvent(Example data) method.

For Example:

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEvent(Example event){
    mainContentList = event.mainContentList;

    if (mViewPager != null) {
        setupViewPager(mViewPager, mainContentList);
    }

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
}

The issue is arises when we call MainActivity from ActivityC,

Now the @Subscribe onEvent(Example data) would not be called because we are posting to it from SplashActivity only, so it won't be called and the ViewPager doesn't gets updated.

What I want:

I want to get the data from StickyPost as early as possible so I can initialize the ViewPager in the OnCreate method of the activity.

I know I can use Application level Singleton or create a DataHolder class etc to store the data but is there anyway to achieve this using EventBus?

Thanks for your time an if you need any more info let me know and I will update.

Atiq
  • 14,435
  • 6
  • 54
  • 69
  • Which `EventBus` and `Subscribe` annotation are those? Those are not Guava's, at least not as of 19.0 – fge Aug 01 '16 at 11:54
  • sorry its [Greenbots EventBus](https://github.com/greenrobot/EventBus) i will add the link to the question – Atiq Aug 01 '16 at 11:55
  • Where did you register to bus and unregister from bus?(`onResume` and `onPause` ?) – nshmura Aug 01 '16 at 13:46
  • @nshmura in `onStart` and `onStop`, but the thing is when we launch `MainActivity` from `SplashActivity` it works but when we launch `MainActivity` from lets say `ActivityC` the `onEvent` wont be called of course so views doesn't gets updated. – Atiq Aug 01 '16 at 15:43
  • You would need to post same event from ActivityC as you do in SplashActivity – Eenvincible Aug 01 '16 at 16:01
  • @Eenvincible yeah but I don't have that data in ActvityC, I think I would use Application level Singleton instead. – Atiq Aug 01 '16 at 16:19
  • Alright, try that then! – Eenvincible Aug 01 '16 at 16:29
  • 1
    `onStart` will not be called if ActivityC is transparent or anther reasons might be. So I recommend you to check onStart is called, and check there is stickyEvent in `EventBus` when `onStart` is called. like this `bus.getStickyEvent(Example.class) != null` – nshmura Aug 01 '16 at 23:31
  • have you heard of passing intents to activities? – Kosh Oct 13 '16 at 06:11
  • @k0sh yup :) but my the kind of data I wanted to pass was little complex for that, anyhow I used a `dataholder` class instead to solve my problem, and actually there is a way to get the `stickevents` in `onCreate` i found out later. – Atiq Oct 13 '16 at 06:15
  • like how complex could it be? more than 1MB? anyway, passing values to an activity where it wasn't even created is bad, EventBus will hold this data until your activity receives it. which is twice the damage. i suggest you use some soft of in device storage, like database for example. – Kosh Oct 13 '16 at 06:17
  • yup database was a good option but i didn't wanted to make the process complex, i was already using `EventBus` so thought if i could use it to achieve this with `EventBus` too. Thanks for the suggestion though I will keep in mind next time :) – Atiq Oct 13 '16 at 06:26

2 Answers2

9

The Solution to problem was pretty simple, we can simple. we call getStickyEvent anywhere, something like this

Example:

EventBus eventbus = Bus.getInstance();

ExampleEvent event = eventbus.getStickyEvent(ExampleEvent.class);

    if ( event != null ){

        // Whatever we want to do with event

        // and then we can remove stickyEvent if we don't need it anymore

        eventbus.removeStickyEvent(event);
    }
Atiq
  • 14,435
  • 6
  • 54
  • 69
0

When you create an event subscriber, you have to register it (and unregister it when it will be destroyed). As state in official documentation, when you work with activity you write :

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

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

If you want to use a singleton to subscribe events, you have to initialize it in a unique point, like application class and register it on eventbus.

xcesco
  • 4,690
  • 4
  • 34
  • 65
  • Am sorry but you didn't get my question I think, am doing all the steps that you mentioned, am able to get the data from `SplashActivity` and create tabs accordingly but when `MainActivity` is called from `ActivityC` the `onEvent` method is not called, hope you got my point – Atiq Aug 01 '16 at 13:28