我想将一些数据从片段父级传递给对话框片段子级。问题是我无法通过bundle传递数据,因为dagger实例化片段对话框,所以bundle总是为null。我正在使用匕首2.12 ..
我是Dagger 2的新手,所以这可能是微不足道的。我仍然试图绕过它。
我按照this教程,并采用它来适应我的应用程序。为了简单起见,我的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;
}
}
这是BaseFragment(由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();
}
}
这是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();
}
}
这是BaseFragmentModule和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);
}
目前,ConformationFragmentDialogModule为空,子组件是基本子组件。
我希望ConformationFragmentDialog可以在不同的片段中重复使用。如何为我调用ConformationFragmentDialog的每个片段设置不同的标题和消息,同时仍以适当的方式处理匕首?
答案 0 :(得分:0)
在ConfirmationFragmentDialog类中使用静态newInstance创建一个片段,如下所示
public class ConformationFragmentDialog extends DialogFragment {
public static ConformationFragmentDialog newInstance(String title,String msg) {
ConformationFragmentDialog fragment = new ConformationFragmentDialog();
// Supply string input as an argument.
Bundle args = new Bundle();
args.putString("BUNDLE_TITLE_KEY", title);
args.putString("BUNDLE_MESSAGE_KEY", msg);
fragment.setArguments(args);
return fragment;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments.getStrings("BUNDLE_TITLE_KEY");
String message = getArguments.getStrings("");
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();
}
}
还可以通过调用newInstance方法返回MainFragment返回片段。