我迁移到导航组件,因为我现在使用 Fragments,所以我不再在 Fragment 中调用 onActivityResult()。
我定义了传递给 AddEditTaskFragment 的参数,并希望将修改后的参数发送回 TodayFragment。由于每次导航到 TodayFragment 时都会调用 onViewCreated(),所以我想检查我是否真的将参数传递给它以将任务保存在我的数据库中。
由于 AddEditTaskFragmentArgs.fromBundle(getArguments()) != null 不起作用,检查片段是否正在接收参数的推荐方法是什么?
这就是我导航回调用片段的方式。
AddEditTaskFragmentDirections.ActionAddEditTaskFragmentToHomeFragment action = AddEditTaskFragmentDirections.actionAddEditTaskFragmentToHomeFragment();
action.setTitle(title);
if (id != -1) action.setId(id);
action.setPriority(priority);
action.setAddanote(duedate);
action.setDuedate(remindme);
action.setRemindme(addanote);
action.setModeEdit(modeEdit);
Navigation.findNavController(getView()).navigate(action);
现在我想做类似的事情:
if (AddEditTaskFragmentArgs.fromBundle(getArguments()) != ...?) {
// Receive data from action
String title = AddEditTaskFragmentArgs.fromBundle(getArguments()).getTitle().trim();
int priority = AddEditTaskFragmentArgs.fromBundle(getArguments()).getPriority();
String duedate = AddEditTaskFragmentArgs.fromBundle(getArguments()).getDuedate();
String remindme = AddEditTaskFragmentArgs.fromBundle(getArguments()).getRemindme();
String addanote = AddEditTaskFragmentArgs.fromBundle(getArguments()).getAddanote().trim();
boolean modeEdit = AddEditTaskFragmentArgs.fromBundle(getArguments()).getModeEdit();
int id = AddEditTaskFragmentArgs.fromBundle(getArguments()).getId();
Task task = new Task(title, addanote, priority, duedate, remindme);
taskViewModel.insert(task);
Toast.makeText(getContext(), "Task saved.", Toast.LENGTH_SHORT).show();
}