简体   繁体   English

如何不使用 Android 中的底部导航栏导航回某些片段

[英]How to not navigate back to certain fragments using a bottom navigation bar in Android

I have a bottom navigation bar with 4 buttons.我有一个带有 4 个按钮的底部导航栏。 One of them is a back-button that just navigates back to the previously displayed fragment (I use a one-activity multiple-fragments approach).其中之一是一个返回按钮,它只是导航回先前显示的片段(我使用单活动多片段方法)。 Basically everything works fine.基本上一切正常。 Now I wanted to know whether it is possible not to navigate back to a certain fragment A?现在我想知道是否可以不导航回某个片段 A? So basically whenever this Fragement A was the last fragment that was being displayed and the user clicks on the back-button, it should not navigate back but rather stay on the current fragment (and maybe display a small toast message).所以基本上每当这个片段 A 是最后一个被显示的片段并且用户点击后退按钮时,它不应该导航回来而是停留在当前片段上(并且可能显示一个小的 toast 消息)。

So here you can see my main activity where the navigation is implemented:所以在这里你可以看到我实现导航的主要活动:

package com.example.td.bapp;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;

import com.example.td.bapp.databinding.ActivityMainBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;


public class MainActivity extends AppCompatActivity  {

   public static DataBaseHelper myDB;
   public static boolean veryFirstCreation = true;


    private ActivityMainBinding binding;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();

        setContentView(view);

        final  NavController navController = Navigation.findNavController(this,
                R.id.navHostfragment);
       NavigationUI.setupWithNavController(binding.bottomNavigation, navController);

        myDB=new DataBaseHelper(this);

        if(veryFirstCreation) {
            navController.navigate(R.id.FR_LanguageSelection);
            veryFirstCreation = false;
        }





          // Define the bahaviour when clicking on items of the bottomNavigationBar; Overwrites the JetpackNavigation behaviour (automatic navigation using the id specified in the BottomNavigation XML menu file)
        binding.bottomNavigation.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        if (item.getItemId()== R.id.BottomNavigation_Back) {
                            //Here I navigate back to the previous fragment
                            navController.navigateUp();

                        }

                        if (item.getItemId()== R.id.BottomNavigation_Language) {
                            navController.navigate(R.id.FR_LanguageSelection);
                        }

                        if (item.getItemId()== R.id.BottomNavigation_MyItems) {
                            navController.navigate(R.id.FR_MyItems);
                        }

                        if (item.getItemId()== R.id.BottomNavigation_Menu) {
                            navController.navigate(R.id.FR_Menu);
                        }



                        return true;
                    }
                });


        };




    }

I'd appreciate every comment and would be quite thankful for your help.我会很感激每一条评论,并非常感谢您的帮助。

You could use the SupportFragmentManager, with this you could know if your back stack contains at least one fragment.您可以使用 SupportFragmentManager,这样您就可以知道您的后台堆栈是否包含至少一个片段。 Use this in your back function (or in your onBackPressed if necessary)在你的背部 function 使用这个(或在你的 onBackPressed 如有必要)

FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() ==1) {
    Log.d("ppl","This is the last fragment. Show your message, poppup or whatever you want here")

} else if (fm.getBackStackEntryCount() >1) {
    Log.d("ppl","the backstack still have more to show")
    fm.popBackStackImmediate();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM