我试图在this之后添加条件导航。但是我遇到了一个问题。 因此,我遵循的步骤是:
navigation drawer
中以MainActivity
作为起始目的地创建了HomeFragment
。FromBlackFragment
导航到BlankFragment
。HomeFragment
从FromBlackFragment
转到up/back button
。问题出在上面的第3步。通过单击HomeFragment
返回back button
,而在BlankFragment
单击上导航到up button
。 第3步的代码是:(在 FromBlackFragment.java 文件中)
requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(),
new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
Navigation.findNavController(view).popBackStack(R.id.homeFragment, false);
}
});
MainActivity.java 文件:
public class MainActivity extends AppCompatActivity {
private NavController navController;
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.my_toolbar);
drawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navController = findNavController(this, R.id.nav_host_fragment);
AppBarConfiguration appBarConfiguration =
new AppBarConfiguration.Builder(navController.getGraph())
.setOpenableLayout(drawerLayout).build();
NavigationUI.setupWithNavController(toolbar, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
return NavigationUI.onNavDestinationSelected(item, navController)
|| super.onOptionsItemSelected(item);
}
}
BlankFragment.java 文件:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SharedPreferences sp = requireActivity().getSharedPreferences("Testing",0);
if(!sp.getBoolean("b",false)){
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("b",true);
editor.apply();
Navigation.findNavController(view).navigate(R.id.fromBlackFragment);
}
}
FromBlackFragment.java 文件:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_from_black, container, false);
}
@Override
public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
requireActivity().getOnBackPressedDispatcher().addCallback(getViewLifecycleOwner(),
new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
Navigation.findNavController(view).popBackStack(R.id.homeFragment, false);
}
});
}
注意:代码中使用的共享首选项不是问题的一部分。
请帮助。谢谢。
编辑:(更具体的问题是)如何同步 upNavigation 和后退按钮?
?答案 0 :(得分:0)
第一步:在MainActivity的onCreate
中,您需要调用setSupportActionBar(工具栏);
第2步:从NavigationUI调用setupActionBarWithNavController(),并传递适当的参数,这将:
设置由以下人员返回的ActionBar 适用于NavController的AppCompatActivity.getSupportActionBar()。
第3步:通过调用setupActionBarWithNavController(),您现在应该覆盖onSupportNavigateUp(),并在调用中进行导航:
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, appBarConfiguration)
|| super.onSupportNavigateUp();
}
如果NavigationUI.navigateUp无法将您带到原始片段目的地,则您可能需要调用明确地将您带到那里的Navigation方法。