【发布时间】:2020-01-31 09:38:02
【问题描述】:
面对 Jetpack Navigation 的一个奇怪问题,我们将不胜感激。我有一个导航图如下:
我想使用喷气背包导航从一个片段导航到另外两个片段。 到目前为止,我只能从 entry_fragment 导航到 second_fragment,如下图所示。
我的 EntryFragment.class :
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_entry,container,false);
v.findViewById(R.id.fragmentOneButton).setOnClickListener(ls);
v.findViewById(R.id.fragmentTwoButton).setOnClickListener(ls);
return v;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
private final View.OnClickListener ls = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.fragmentOneButton:
Toast.makeText(getActivity().getApplicationContext(), "First Button", Toast.LENGTH_SHORT).show();
Navigation.createNavigateOnClickListener(R.id.action_entry_fragment_to_first_fragment).onClick(view);
break;
case R.id.fragmentTwoButton:
Toast.makeText(getActivity().getApplicationContext(), "Second Button", Toast.LENGTH_SHORT).show();
Navigation.createNavigateOnClickListener(R.id.action_entry_fragment_to_second_fragment).onClick(view);
break;
}
}
};
在第一个按钮单击时,我只得到了祝酒词,在单击第二个按钮时,我实际上可以导航到 SecondFragment。我在这里做错了什么?以及使用 Navigation 进行导航的最佳做法是什么。
提前致谢
【问题讨论】:
标签: java android navigation android-jetpack