user6441210
user6441210

Reputation:

Using Otto in Fragment and Service class

I have a RecyclerView inside a Fragment class, and I have a Service class where I wanted to add Item into my RecyclerView. The problem is I have no any idea on how can I possibly do that. So I've asked and someone told me to use Otto. And now I'm stock because it's not working. Maybe there's something wrong with my code, i don't know, This is the first time I used Otto.

This is how I implement My Fragment Class

public static class MyFragment extends Fragment {

    RecyclerView recyclerView;
    LinearLayoutManager linearLayoutManager;
    static TheRecyclerAdapter theRecyclerAdpater;
    private List<TheData> theDataList;
    public static Bus bus = new Bus(ThreadEnforcer.MAIN);

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.online_devices, container, false);

        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);

        theDataList = new ArrayList<>();
        theRecyclerAdpater = new TheRecyclerAdapter(getActivity().getBaseContext(), theDataList);

        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(theRecyclerAdpater);

        bus.register(this); //I already register my fragment
        getActivity().startService(new Intent(getActivity(),MyService.class)); //I call the Service

        return view;
    }

    //I already Subscribe, I am expecting to execute this Function but nothings happened
    @Subscribe
    public void getMessage(String s) {
        TheData a = new TheData(s);
        theDataList.add(a);
        theRecyclerAdpater.notifyDataSetChanged();
    }
}

And this is My Service Class

public class ServerHelperService extends Service {
    public static Bus bus = new Bus(ThreadEnforcer.MAIN);

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate(){
        bus.post("a"); //This where I'm expecting to call the getMessage function in my fragment
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_NOT_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

Does anyone has an idea why it's not working?

Upvotes: 1

Views: 249

Answers (1)

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29834

First thing first, you must grasp the concept of event bus. In Otto doc, you can only see technical details about how event bus working in text. So for explanation sake, we borrow from EventBus. Here the images explaining how event bus working: EventBus Description Here are 3 parts:

  1. Producer, the producer of event.
  2. Event, the event that happens which producer send.
  3. Subscriber, the subcriber to event which receiving the event.

Producer can only sent event. So you need to sent only event. Of course you can add 'payload' to event. So, to send the event, you can use something like:

bus.post(new SampleEvent("Your Data"));

Event is only a pojo class, something like this:

public class SampleEvent {
  private String message;
  public SampleEvent(String message) {
    this.message = message;
  }

  public getMessage() {
     return message;
  }
}

or even a blank class:

public class BlankEvent {
}

Then you should Subscribe for waiting and receiving the event with:

@Subscribe
public void getMessage(SampleEvent event) {
  String message = event.getMessage();
  // Process the message here.
}

I think, by reading this simple explanation you will understand what are the problems in your code.

But I suggesting you use EventBus instead. OttoBus is deprecated now. Don't be afraid of the learning curve, because they share the same concept.

Upvotes: 1

Related Questions