how to disable bottom navigation for some fragment

Viewed 338

Hello I have added a bottom navigation in main activity and rest all of the layouts are fragments now I have a login fragment that I don't want to show the bottom navigation and I don't even want to include bottom navigation to each fragment separately, is there any method where I can disable bottom navigation in some fragments like login fragment

if you need more reference please tell me i will update the question

Main_Activity.java

public class MainActivity extends AppCompatActivity {

    BottomNavigationView bottomNavigationView;
    Deque<Integer> integerDeque = new ArrayDeque<>(4);
    boolean flag = true;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        setContentView(R.layout.activity_main);
        Window window = this.getWindow();
        window.setStatusBarColor(this.getResources().getColor(R.color.black));
        bottomNavigationView = findViewById(R.id.bottom_navigation_view);
        integerDeque.push(R.id.nav_home);
        loadFragments(new Home_Fragment());
        bottomNavigationView.setSelectedItemId(R.id.nav_home);

        bottomNavigationView.setOnNavigationItemSelectedListener(
                item -> {
                    int id = item.getItemId();
                    if (integerDeque.contains(id)) {
                        if (id == R.id.nav_home) {
                            integerDeque.size();
                            if (flag) {
                                integerDeque.addFirst(R.id.nav_home);
                                flag = false;
                            }
                        }
                        integerDeque.remove(id);
                    }
                    integerDeque.push(id);
                    loadFragments(getFragment(item.getItemId()));
                    return false;
                }
        );

    }

    @SuppressLint("NonConstantResourceId")
    private Fragment getFragment(int itemId) {
        switch (itemId) {
            case R.id.nav_home:
                bottomNavigationView.getMenu().getItem(0).setChecked(true);
                return new Home_Fragment();
            case R.id.nav_following:
                bottomNavigationView.getMenu().getItem(1).setChecked(true);
                return new Following_Fragment();
            case R.id.nav_upload:
                bottomNavigationView.getMenu().getItem(2).setChecked(true);
                return new Upload_Fragment();
            case R.id.nav_notification:
                bottomNavigationView.getMenu().getItem(3).setChecked(true);
                return new Notification_Fragment();
            case R.id.nav_profile:
                bottomNavigationView.getMenu().getItem(4).setChecked(true);
                return new Profile_Fragment();
        }
        bottomNavigationView.getMenu().getItem(0).setChecked(true);
        return new Home_Fragment();
    }

    public void loadFragments(Fragment fragment) {
        if (fragment != null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, fragment, fragment.getClass().getSimpleName())
                    .commit();
        }
    }

    @Override
    public void onBackPressed() {
        integerDeque.pop();
        if (!integerDeque.isEmpty()) {
            loadFragments(getFragment(integerDeque.peek()));
        } else {
            finish();
        }
    }
}

Update 1 // adedd login_fragment according to the answer but its not working

Login_Fragment.java

    public class Login_Fragment extends Fragment {
        @Nullable
        @org.jetbrains.annotations.Nullable
        @Override
        public View onCreateView(@NonNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
 if (getActivity() != null && getActivity() instanceof MainActivity)
            ((MainActivity) getActivity()).bottomNavigationView.setVisibility(View.GONE);
            return inflater.inflate(R.layout.login_fragment, container, false);
        }
    }

Update 2:- OK the method given in the answer is working but when I go to the previous fragment the bottom nav diapered for all the fragments or I say for entire app there is no bottom nav

2 Answers

you can do bellow in your fragment:

BottomNavigationView navBar = getActivity().findViewById(R.id.bottomBar);
navBar.setVisiblity(View.GONE);

or

 public class Login_Fragment extends Fragment {
    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
    if (getActivity() != null && getActivity() instanceof MainActivity)
            ((MainActivity) getActivity()).bottomNavigationView.setVisibility(View.GONE);
   return inflater.inflate(R.layout.login_fragment, container, false);
       
    }
}

Hello I have added a bottom navigation in main activity and rest all of the layouts are fragments now I have a login fragment that I don't want to show the bottom navigation and I don't even want to include bottom navigation to each fragment separately ,is there any method where I can disable bottom navigation in some fragments like login fragment

So, you don't want to handle that on every fragments, but only on the fragments that you want to make the BottomNavigationView hidden.

@Ali A. Jalil provided part of the answer for hiding the BottomNavigationView while accessing the login fragment; the same way you can show it again, when this fragment is destroyed in the opposite fragment lifecycle method to that you hide it in; i.e. You've hidden it in onCreateView(), then you need to show it in onDestroyView().

 public class Login_Fragment extends Fragment {
    @Nullable
    @org.jetbrains.annotations.Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        ((MainActivity) requireActivity()).bottomNavigationView.setVisibility(View.GONE);
        return inflater.inflate(R.layout.login_fragment, container, false);
    }
    
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ((MainActivity) requireActivity()).bottomNavigationView.setVisibility(View.VISIBLE);        
    }   
}   

This way you have the same state in terms of BottomNavigationView visibility when you exit the login fragment as you entered it.

Related