Android Jetpack导航组件常规导航问题 [英] Android Jetpack Navigation component Condtional navigation issue

查看:112
本文介绍了Android Jetpack导航组件常规导航问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在之后添加条件导航.但是我遇到了一个问题.因此,我遵循的步骤是:

I was trying to add conditional navigation following this. But I ran into a problem. So the steps I followed are:

  1. MainActivity 中创建了导航抽屉,并以 HomeFragment 作为起始目的地.
  2. 有条件地从 BlankFragment 导航到 FromBlackFragment .
  3. 要通过单击上/后按钮 FromBlackFragment 转到 HomeFragment .
  1. Created navigation drawer in the MainActivity with HomeFragment as a start destination.
  2. Conditionally navigated to FromBlackFragment from BlankFragment.
  3. Want to go to HomeFragment from FromBlackFragment by clicking up/back button.

问题出在上面的第3步.我将通过单击后退按钮 回到 HomeFragment ,而在 up按钮上导航到 BlankFragment 点击.第3步的代码是:(在 FromBlackFragment.java 文件中)

The problem is with step 3 above. I am going back to HomeFragment by clicking back button whereas I am navigated to BlankFragment on up button click. The code for step 3 is: (inside FromBlackFragment.java file)

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);
                    }
                });
    }

注意:代码中使用的共享首选项不是问题的一部分.

Note: Shared preferences used in the code is not part of the problem.

请帮助.谢谢.

(更具体地说,问题是)如何同步 upNavigation 后退按钮?

(More specifically the question is) How to sync upNavigation and back button?

推荐答案

第一步:在MainActivity的 onCreate 中,您需要调用

Step1: In MainActivity's onCreate, you need to call setSupportActionBar(toolbar);

第二步:调用

Step2: Call setupActionBarWithNavController() from NavigationUI, passing in the proper arguments, this will:

设置由以下人员返回的ActionBar与NavController一起使用的AppCompatActivity.getSupportActionBar().

Set up the ActionBar returned by AppCompatActivity.getSupportActionBar() for use with a NavController.

第3步:通过调用setupActionBarWithNavController(),您现在应该覆盖 onSupportNavigateUp (),插入呼叫以进行导航:

Step3: By calling setupActionBarWithNavController(), you should now override onSupportNavigateUp(), putting in the call to navigate:

   @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, appBarConfiguration)
                || super.onSupportNavigateUp();
    }

如果NavigationUI.navigateUp无法将您带到原始片段目的地,则您可能需要调用明确地将您带到那里的Navigation方法.

If NavigationUI.navigateUp won't get you to home fragment destination, you maybe want call a Navigation method that explicitly get you there.

这篇关于Android Jetpack导航组件常规导航问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆