How does eventbus register the onevent method befo

2019-05-30 10:05发布

The listItemFragment post an event to the ItemDetailFragment to refresh the UI. BUt the ItemDetailFragment is never used before, so the register() method in ItemDetailFragment is never invoked. So I want to know how the posted event in listItemFragment know the onevent method in ItemDetailFragment. Here is the source code:

package com.angeldevil.eventbusdemo;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.angeldevil.eventbusdemo.Event.ItemListEvent;

import de.greenrobot.event.EventBus;

public class ItemListFragment extends ListFragment
{

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    // Register
    EventBus.getDefault().register(this);
}

@Override
public void onDestroy()
{
    super.onDestroy();
    // Unregister
    EventBus.getDefault().unregister(this);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
    super.onViewCreated(view, savedInstanceState);

    new Thread()
    {
        public void run()
        {
            try
            {
                Thread.sleep(2000); 

                EventBus.getDefault().post(new ItemListEvent(Item.ITEMS));
            } catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        };
    }.start();
}

public void onEventMainThread(ItemListEvent event)
{
    setListAdapter(new ArrayAdapter<Item>(getActivity(),
            android.R.layout.simple_list_item_activated_1,
            android.R.id.text1, event.getItems()));
}

@Override
public void onListItemClick(ListView listView, View view, int position,
        long id)
{
    super.onListItemClick(listView, view, position, id);
    EventBus.getDefault().post(getListView().getItemAtPosition(position));
}

 }


ItemDetailFragment:

package com.angeldevil.eventbusdemo;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import de.greenrobot.event.EventBus;

    public class ItemDetailFragment extends Fragment
{

private TextView tvDetail;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    // register
    EventBus.getDefault().register(this);
}

@Override
public void onDestroy()
{
    super.onDestroy();
    // Unregister
    EventBus.getDefault().unregister(this);
}


public void onEventMainThread(Item item)
{
    if (item != null)
        tvDetail.setText(item.content);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.fragment_item_detail,
            container, false);
    tvDetail = (TextView) rootView.findViewById(R.id.item_detail);
    return rootView;
}
}

1条回答
等我变得足够好
2楼-- · 2019-05-30 10:34

The GreenRobot EventBus uses reflection to find methods called onEvent and delivers the event object to those with a matching signature.

查看更多
登录 后发表回答