0

I create an application and use a BottomNavigationView in it and one of the fragments calls the other fragment through the adapter. I am trying to make the BottomNavigationView disappear when this fragment is called, for this I use setVisibility (). But the problem is that it doesn’t disappear, maybe someone can tell me what I’m doing wrong

BottomNavigationView:

public class ProfileFragment extends Fragment {
    private ImageView avatar;
    private TextView nickname, bio, website;
    private FirebaseMethods mFirebaseMethods;
    private Context mContext;
    private AppBarLayout mAppBarLayout;
    private Menu mMenu;
    private RecyclerView mRecyclerView;
    private FirebaseUser mCurrentUser;
    private TextView followers, followings;
    private BottomNavigationView mBottomNavigationView;


    private String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};
    private ActivityResultContracts.RequestMultiplePermissions mRequestMultiplePermissions= new ActivityResultContracts.RequestMultiplePermissions();

    private DatabaseReference mDatabaseReference;
    private DatabaseReference followingRef;
    private DatabaseReference followersRef;

    private ReadWritePermissions mReadWritePermissions;
    private IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);

    private String mParam1;
    private String mParam2;

    public ProfileFragment() {
        // Required empty public constructor
    }

    public static ProfileFragment newInstance(String param1, String param2) {
        ProfileFragment fragment = new ProfileFragment();
        Bundle args = new Bundle();

        fragment.setArguments(args);
        return fragment;
    }

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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_profile2, container, false);

        mContext = container.getContext();
        mFirebaseMethods = new FirebaseMethods(mContext);
        mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
        mBottomNavigationView = getActivity().findViewById(R.id.bottomNavigation);

        followingRef = FirebaseDatabase.getInstance().getReference("Following");
        followersRef = FirebaseDatabase.getInstance().getReference("Followers");

        Toolbar toolbar = view.findViewById(R.id.toolbar);
        mAppBarLayout = view.findViewById(R.id.app_bar);
        mRecyclerView = view.findViewById(R.id.posts_recycler);

        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
        ((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);

        setHasOptionsMenu(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().finish();
            }
        });

        loadPosts();

        avatar = view.findViewById(R.id.profile_avatar);
        nickname = view.findViewById(R.id.profile_nickname);
        bio = view.findViewById(R.id.profile_bio);
        website = view.findViewById(R.id.profile_website);
        followers = view.findViewById(R.id.followers_count);
        followings = view.findViewById(R.id.following_count);

        setFollowingFollowers();

        followers.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FollowersFollowingsFragment fragment = new FollowersFollowingsFragment();
                Bundle args = new Bundle();
                args.putString("uid", mCurrentUser.getUid());
                args.putString("followingsOrFollowers", "followers");
                fragment.setArguments(args);

                FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
                fragmentTransaction.add(R.id.frame_container,fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });

        followings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FollowersFollowingsFragment fragment = new FollowersFollowingsFragment();
                Bundle args = new Bundle();
                args.putString("uid", mCurrentUser.getUid());
                args.putString("followingsOrFollowers", "followers");
                fragment.setArguments(args);

                FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
                fragmentTransaction.add(R.id.frame_container,fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });

        avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        mFirebaseMethods.getUserDataProfile(avatar, nickname, bio);

        return view;
    }

    private void loadPosts()
    {
        mReadWritePermissions = new ReadWritePermissions(getActivity().getLifecycle(), getActivity().getActivityResultRegistry());
        getLifecycle().addObserver(mReadWritePermissions);

        mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
        mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Posts");
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        FirebaseRecyclerOptions<Posts> options
                = new FirebaseRecyclerOptions.Builder<Posts>()
                .setQuery(mDatabaseReference.orderByChild("uid").equalTo(mCurrentUser.getUid()), Posts.class)
                .build();


        mReadWritePermissions = new ReadWritePermissions(getActivity().getLifecycle(), getActivity().getActivityResultRegistry());
        getLifecycle().addObserver(mReadWritePermissions);

        PostAdapter adapter = new PostAdapter(options, mContext, mReadWritePermissions);
        mRecyclerView.setAdapter(adapter);
        adapter.startListening();
    }

    private void setFollowingFollowers()
    {
        mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
        followersRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                followers.setText(Integer.toString((int)snapshot.child(mCurrentUser.getUid()).getChildrenCount()));
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

        followingRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                followings.setText(Integer.toString((int)snapshot.child(mCurrentUser.getUid()).getChildrenCount()));
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}
lin
  • 1
  • 1
  • 3

2 Answers2

0

Use setVisibility(View.GONE) instead.

I had the same problem, for some reason it doesn't work, but in the docs they also use View.GONE.

EDIT: Use the method shown in the docs to hide/show BottomNavigationView depending on the Fragment.

navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
   @Override
   public void onDestinationChanged(@NonNull NavController controller,
           @NonNull NavDestination destination, @Nullable Bundle arguments) {
       if(destination.getId() == R.id.full_screen_destination) {
           toolbar.setVisibility(View.GONE);
           bottomNavigationView.setVisibility(View.GONE);
       } else {
           toolbar.setVisibility(View.VISIBLE);
           bottomNavigationView.setVisibility(View.VISIBLE);
       }
   }
});

Use Navigation component like in this example.

cmak
  • 576
  • 1
  • 4
  • 15
0

I fixed this by using DataBinding and ObservableField for the visibility, I don't why it didn't work through the normal onDestinationChangedListener on the NavController, it just bluntly ignored that command, everything else in that block worked fine but not the visibility.

So an ObservableField in the databinding did the trick

android:visibility="@{hideBottomNav ? View.GONE : View.VISIBLE}"

And set the value to the observable field in the onDestinationChangedListener to your desired outcome

 if(destination.id == R.id.navigation_your_notes){
        hideBottomNav.set(true)
    } else hideBottomNav.set(false)