I am working on an app which uses the NavigationDrawer. Different fragments are placed into the content view of the MainActivity
whenever a menu item in the drawer is selected.
To inform the MainActivity
that a Fragment
successfully attached the following callback is executed:
public class CustomFragment extends Fragment {
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached();
}
}
Since I am started using Otto with Dagger in the project I am curious how I can substitute the callback with a .post()
event such as:
mBus.post(new CustomFragmentAttachedEvent);
The problem is that mBus
is null
in onAttach()
. It gets initialized in onCreate()
.
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((MyApp) getActivity().getApplication()).getObjectGraph().inject(this);
}
Here is an example of such a Fragment class.
References:
You can easily try out the example yourself: Create a new project from the NavigationDrawer
template available in Android Studio, add Dagger and Otto and try to substitute the mentioned callback.
Working solution:
- Here is the example Fragment in the final working version.