을 작성하여 Observer pattern
을 사용하여 Android Fragments
및 Activity
에 사용할 수 있습니다.조각이 보이는 시점을 알 수있는 방법
Subscribe
여러 파편에 연결된 여러 수신기가 있습니다. 조각을 볼 때
내 사용자 지정 이벤트 리스너는 다음을 수행합니다 :
- 는 조각에 대한
subscribed
된 리스너를find all
것이다. - 해당
fragment
에 대해pending event
을 확인하십시오. Fire
어떤pending event
도 해당fragment
과 관련됩니다.
조각이나 활동이 현재보기에있는 경우 어떻게 알 수 있습니까? 그 조각이나 활동이 보일 때 곧 보류중인 이벤트를 발생시킬 수 있습니까?
내 구현 요약 :
//Defining the Interface
public class MyCustomObject {
// Step 1 - This interface defines the type of messages I want to communicate to my owner
public interface MyCustomObjectListener {
// These methods are the different events and
// need to pass relevant arguments related to the event triggered
public void onDataChange(MyCustomObject obj);
// or when data has been loaded
public void onDataDestroy(MyCustomObject obj);
}
}
// Create Listener Setter
public class MyCustomObject {
// Step 2 - This variable represents the listener passed in by the owning object
// The listener must implement the events interface and passes messages up to the parent.
private MyCustomObjectListener listener;
// Constructor where listener events are ignored
public MyCustomObject() {
// set null or default listener or accept as argument to constructor
this.listener = null;
}
// Assign the listener implementing events interface that will receive the events
public void setCustomObjectListener(MyCustomObjectListener listener) {
this.listener = listener;
}
}
//Implement Listener Callback
public class MyParentActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
// Create the custom object
MyCustomObject object = new MyCustomObject();
// Step 4 - Setup the listener for this object
object.setCustomObjectListener(new MyCustomObject.MyCustomObjectListener() {
@Override
public void onDataChange(MyCustomObject obj) {
// Code to handle on data change event
}
@Override
public void onDataDestroy(MyCustomObject obj) {
//Code to handle onDestroy event.
}
});
}
}
문제 이제
가에 subscribed
를 얻을 ---- Event1
가정 --->Fragment1 view
-----와 같은 Event1
은 ----> fragment2보기에서 fired/publish
을 얻습니다.
그러면 when does Fragment1 get in view again
을 어떻게 알 수 있습니까? 그러면 Fragment1이 표시되면 fire
Event1
이 표시됩니다. 나는 도서관을 구현하고
따라서 나는 우리가 각 조각/활동 이벤트 버그를 등록해야 첫번째 GreenRobot
처럼 구현 여러 EventBus 라이브러리에 무슨 일에 가까운 솔루션을 필요로 참고
.
//Define events
public class MessageEvent { /* Additional fields if needed */ }
//Prepare subscribers: Register your subscriber (in your onCreate or in a constructor):
eventBus.registerSticky(this);
//Declare Subscriber method
@Subscribe
public void onDataChange(MessageEvent obj) {/* Do something */};
//Post events:
eventBus.postSticky(event);
어떻게 조각이보기 및 화재 행사에 얻을 때 EventBus에서 postSticky 이벤트 구현 즉시 조각이보기에 얻을로 알고 않습니다.?