如何知道片段何时在视图中

How to know when a fragment is in view

我正在创建一个 Event Listener Library 使用 Observer pattern 用于 Android FragmentsActivity

将有一个方法Subscribe在多个片段中关联多个侦听器。

When a fragment is visible my custom event listener will do following:

  1. find all 已成为该片段 subscribed 的侦听器。
  2. 检查 fragment 的任何 pending event
  3. Firefragment 关联的任何 pending event

我如何知道片段或 Activity 何时在当前视图中。这样我就可以在该片段或 activity 进入视图时立即触发未决事件?

My implementation summary:

//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.
            }
        });
    }
}

问题

现在假设一个 Event1 ----在 ---> Fragment1 view ----- 中得到 subscribed 并且相同的 Event1 得到 fired/publish 在----> fragment2 view

那么我怎么知道 when does Fragment1 get in view again 以便当 Fragment1 进入视图时我可以 fire Event1

注意

我正在实现一个库,因此我需要一个接近于实现多个 EventBus 库(如 GreenRobot

时发生的情况的解决方案

首先我们必须在每个 fragment/activity.

中注册事件错误
 //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 事件实现如何知道片段何时进入视图并在片段进入视图时立即触发事件?

1.我怎么知道片段或 Activity 何时在当前视图中。这样我就可以在该片段或 activity 进入视图时立即触发未决事件?

不用担心片段或 Activity 何时在视图中。利用组件的生命周期。在 oncreate 或 onResume 上注册并在 onDestroy 或 unpause 上注销订阅。

2。 EventBus 中的 postSticky 事件实现如何知道片段何时进入视图并在片段进入视图时立即触发事件?

EventBus 将事件存储在队列中。当组件注册时,发布者将事件传递给组件的匹配方法。