I want to pass some data from fragment parent to dialog fragment child. The trouble is I cannot pass the data through bundle since dagger instantiates fragment dialog, so the bundle is always null. I am using dagger 2.12..
I'm new to Dagger 2, so this might be something trivial. I am still trying to wrap my head around it.
I followed this tutorial, and adopted it to suit my application. To keep it simple here is my MainFragment:
public class MainFragment extends BaseFragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
this.setHasOptionsMenu(true);
return inflater.inflate(R.layout.my_fragment, container, false);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.my_menu, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_delete:
Bundle args = new Bundle();
args.putString("BUNDLE_TITLE_KEY", getResources().getString(R.string.dialog_delete_name));
args.putString("BUNDLE_MESSAGE_KEY", getResources().getString(R.string.delete_message));
addDialogFragment(new ConformationFragmentDialog(), args);
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
}
Here is BaseFragment (extended by MainFragment):
public class BaseFragment extends Fragment implements HasFragmentInjector {
@Inject
@Named(BaseFragmentModule.CHILD_FRAGMENT_MANAGER)
FragmentManager childFragmentManager;
@Inject
DispatchingAndroidInjector<Fragment> childFragmentInjector;
@Override
public void onAttach(Context context) {
AndroidInjection.inject(this);
super.onAttach(context);
}
@Override
public AndroidInjector<Fragment> fragmentInjector() {
return childFragmentInjector;
}
protected final void addDialogFragment(DialogFragment dialogFragment, Bundle bundle) {
dialogFragment.setArguments(bundle);
childFragmentManager.beginTransaction().add(dialogFragment, "Hello").commit();
}
}
And here is ConformationFragmentDialog:
public class ConformationFragmentDialog extends DialogFragment {
@Override
public void onAttach(Context context) {
AndroidInjection.inject(this);
super.onAttach(context);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
dialogBuilder.setNegativeButton(R.string.common_odustani, new DialogInterface.OnClickListener() {
//TODO: set title and message dynamically, bundle is null
}).setTitle("TITLE").setMessage("MESSAGE");
return dialogBuilder.create();
}
}
Here are BaseFragmentModule and MainFragmentModule:
@Module
public abstract class BaseFragmentModule {
public static final String FRAGMENT = "BaseFragmentModule.fragment";
public static final String CHILD_FRAGMENT_MANAGER = "BaseFragmentModule.ChildFragmentManager";
@Provides
@Named(CHILD_FRAGMENT_MANAGER)
static FragmentManager childFragmentManager(@Named(FRAGMENT) Fragment fragment) {
return fragment.getChildFragmentManager();
}
}
@Module(includes = BaseFragmentModule.class, subcomponents = ConformationFragmentDialogSubcomponent.class)
public abstract class MainFragmentModule {
@Binds
@IntoMap
@FragmentKey(ConformationFragmentDialog.class)
abstract AndroidInjector.Factory<? extends Fragment> conformationFragmentDialog(ConformationFragmentDialogSubcomponent.Builder builder);
@Binds
@Named(BaseFragmentModule.FRAGMENT)
abstract Fragment fragment(UvjetListFragment uvjetListFragment);
}
At this moment, ConformationFragmentDialogModule is empty and Subcomponents are basic subcomponents.
I want ConformationFragmentDialog to be reusable in different fragments. How can I set different title and message for every fragment from which I am calling ConformationFragmentDialog, while still doing things in dagger appropriate manner?