popBackStackImmediate之后事件总线onEvent不响应

时间:2019-06-15 06:53:56

标签: android android-fragments greenrobot-eventbus

我有两个片段。从第一个CreateFragment开始,我通过一个按钮打开LocationPickerFragment。在第二个片段中,我有一些信息,单击菜单按钮后,我需要发布事件并运行popBackStackImmediate。返回第一个片段后,onEvent方法不起作用。

第一个片段

public class CreateFragment extends Fragment {

private View view;
private FusedLocationProviderClient fusedLocationClient;
private Location currentLocation;

@BindView(R.id.address)
TextView address;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_create, container, false);
    ButterKnife.bind(this, view);
    setHasOptionsMenu(true);
    fusedLocationClient = LocationServices.getFusedLocationProviderClient(getContext());
    return view;
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    EventBus.getDefault().register(this);
}

@Override
public void onDetach() {
    super.onDetach();
    EventBus.getDefault().unregister(this);
}

@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEvent(com.user.app.model.Location location) {
    System.out.println("entered?"); // never gets here
    address.setText(location.getAddress());
}

第二个片段(在第一个片段中称为)

public class LocationPickerFragment extends Fragment {

private View view;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_location_picker, container, false);
    ButterKnife.bind(this, view);
    setHasOptionsMenu(true);
    setMap(savedInstanceState);
    return view;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.location_upbar, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.action_location_back) {
        getFragmentManager().popBackStackImmediate();
    } else if (item.getItemId() == R.id.action_location_check) {
        EventBus.getDefault().postSticky(new Location().setAddress("some location"))); // this location is my model class not the original one
        getFragmentManager().popBackStackImmediate();
    }
    return super.onOptionsItemSelected(item);
}
}

0 个答案:

没有答案