Skip to content
This repository has been archived by the owner on Apr 30, 2019. It is now read-only.

event from activity to fragment #155

Open
ziahaqi opened this issue May 21, 2015 · 1 comment
Open

event from activity to fragment #155

ziahaqi opened this issue May 21, 2015 · 1 comment

Comments

@ziahaqi
Copy link

ziahaqi commented May 21, 2015

i have a problem when send event from activity (with post/produce) to a fragment. the event will be taken from binded service if the activity recevied notification via brodcast receiver when the service received a message.
Activity{
private SMS sms;
...
@OverRide
protected void onCreate(@nullable Bundle savedInstanceState) {
...
smsFragment = new TransactionSMSFragment();
getSupportFragmentManager().beginTransaction().add(R.id.stack_container, smsFragment, RegisterServerListFragment.TAG).commit();
}

private BroadcastReceiver smsReceiver = new BroadcastReceiver() {
....
@OverRide
public void onReceive(Context context, Intent intent) {
if(DEBUG)Log.i(TAG, "sms>reciver");
if(intent != null){
SMS sms = smsService.getReceivedMessege();
this.sms = sms;
EventBus.getInstance().post(sms);
}
}
};
@produce
public String produce(){
return this.sms;
}

  • the otto register on create and unregistered on destroy
  • and the fragment
    Fragment{
    ...
    @subscribe
    public void subscribeSMS(SMS sms){
    Log.d("bus", "sms:" + sms );
    }
    }

what wrong with those code ? how actually otto work ? thank's

@EminYahyayev
Copy link

From EventBus.getInstance().post(sms); it seems to me that you're mixing Otto with Greenrobot's EventBus. @Produce annotation is only provided by Otto. If you want to use Otto you can do like this:

public class OttoBus {
        private static Bus bus;

        public static Bus getInstance(){
            if (bus == null)
                bus = new Bus();

            return bus;
        }
    }

and later OttoBus.getInstance().post(sms);.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants