Robins Gupta
Robins Gupta

Reputation: 3153

How to know when a fragment is in view

I am creating an Event Listener Library using an Observer pattern to be used in Android Fragments and Activity.

There will be a method to Subscribe several listener associated in several Fragments.

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

  1. Will find all the listener that has been subscribed for that fragment.
  2. Check for any pending event for that fragment.
  3. Fire any pending event associated for that fragment.

How can i know when a fragment or Activity is in current view. So that i can fire pending events as soon a that fragment or activity get in view ?

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

Problem

Now suppose a Event1 ----gets subscribed in ---> Fragment1 view ----- and the same Event1 gets fired/publish in----> fragment2 view

So how will i know when does Fragment1 get in view again so that i can fire the Event1 as soon as Fragment1 comes in View.

NOTE

I am implementing a library thus i need a solution close to what happens in implementation several EventBus library like GreenRobot

Where first we have to register for event bug in each 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);

How does postSticky events implementation in EventBus knows when a fragment get in a view and fire events as soon as fragments get in the view.?

Upvotes: 3

Views: 238

Answers (1)

masp
masp

Reputation: 515

1. How can i know when a fragment or Activity is in current view. So that i can fire pending events as soon a that fragment or activity get in view ?

Well don't worry about when a fragment or Activity is in View. Take advantage of the lifecycle of the components. Register on the oncreate or onResume and unregister the subscription on the onDestroy or unpause.

2. How does postSticky events implementation in EventBus knows when a fragment get in a view and fire events as soon as fragments get in the view.?

EventBus stores the events in a queue. When a component registers the publisher delivers the events to the matching methods of the components.

Upvotes: 1

Related Questions