2015-01-09 2 views
2

GreenBot EventBus를 사용하여 하나의 활동에서 다른 활동으로 이벤트를 전달합니다.Greenrobot EventBus 이벤트가 수신되지 않았습니다.

흐름은 다음과 같습니다. Activity1이 시작 -> 바코드 스캔 -> Activity2가 시작됨 -> 응답을 수락 또는 거절하고 Activity1에 이벤트를 보냅니다. activity1에에서

@Override 
public void onCreate(){ 
    EventBus.getDefault().register(this); 
    // other initialization code 
    EventBus.getDefault().post(new MyEvent()); 
} 

내가 이벤트 버스를 등록하고 또한 내가 이벤트를 수신 (myEvent가 myEvent가) 방법 대중의 onEvent 있습니다

그래서 activity2에 같은 일을 수행하여 activity1에 대한 새로운 이벤트를 보냅니다.

문제는 onEvent가 트리거되지 않는다는 것입니다. 이벤트 버스 객체 (예 : 활동 1과 2에서 서로 다른 인스턴스 또는 someting)에 문제가있을 수 있지만 동일한 인스턴스가 있는지 살펴 보았습니다.

나는 무엇이 문제인지 모릅니다. 누군가가 한 번 봐서 내가 뭘 잘못하고 있는지 말해 준다면, 나는 그것을 크게 고맙게 여길 것이다.

감사합니다.

+0

귀하의 활동에서 eventbus 등록 취소는 어디에서 했습니까? – Krish

+0

https://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent,%20int) - https://stackoverflow.com/을 확인하기 만하면됩니다. 질문/10407159/어떻게 시작해야할까요? – deive

답변

3

이 경우에는 끈적 거리는 이벤트를 사용해야 할 수도 있습니다. Activity1이 Activity2를 시작하면 배경으로 이동하고 더 이상 이벤트를 수신 할 수 없습니다.

등록합니다. 대신 EventBus.getDefault() 당신의 activity1에에

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

을 (개체 이벤트)이 넣고

EventBus.getDefault().postSticky(new MyEvent()); 

Here is a link to the documentation explaining it

와 activity2에의

EventBus.getDefault().post(new MyEvent()); 

교체

1

Activity1 EventBus 등록 취소는 어떻게 생겼습니까?

는 내가이 일을 하였다 있기 때문에 동일한 문제가 있었다 :

Activity1.java 

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

이의 문제는 당신이 activity2에 onStop를 시작할 때, 따라서 이벤트에 대한 구독을 제거, 전화를 가져옵니다이다. 등록 취소를 onDestroy로 옮겨서 해결할 수있었습니다.

Activity1.java 

@Override 
protected void onDestroy() { 
    super.onDestroy(); 
    EventBus.getDefault().unregister(this); 
} 
관련 문제