Search code examples
androidandroid-fragmentsbottomnavigationview

Acces BottomNavigationView item from onItemClick in a fragment


I have a MainActivity with a BottomNavigationView with 3 items in it. When I first open the app, only one is enable, the first one.

I have a RecyclerView on the first fragment (the first item in the menu). When I click on one item of the RecyclerView, I want to switch to the second Fragment and to enable the second and third item.

I am able to switch to the second Fragment, but I can not set the items enable again.

The first Fragment with the RecyclerView is:

public class Homefragment extends Fragment implements OnItemClickListener {

    private ArrayList<fileItem> mfileList = new ArrayList<>();
    private RecyclerView mRecyclerView;
    private fileAdapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;

    private BottomNavigationView bottomNav;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_home, container, false);

        Context context = getActivity();
        bottomNav = view.findViewById(R.id.bottom_navigation);

        // Listing all text files
        String path = Environment.getExternalStorageDirectory().toString()+"/FlexiCounts";
        File directory = new File(path);
        File[] filesList = directory.listFiles(new FilenameFilter() {
            public boolean accept(File directory, String name) {
                return name.toLowerCase().endsWith(".txt");
            }
        });

        setRecyclerView(view, filesList);

        return view;
    }

    private void setRecyclerView(View view, File[] files){

        if (files.length > 0) {

            for (int i = 0; i < files.length; i++) {
                mfileList.add(new fileItem(R.drawable.ic_account_balance_black_24dp, files[i].getName().replace(".txt", ""), "25-03-2020", "07-04-2020"));
            }

            mRecyclerView = view.findViewById(R.id.fileRecycler);
            mRecyclerView.setHasFixedSize(true);
            mLayoutManager = new LinearLayoutManager(getActivity());
            mAdapter = new fileAdapter(mfileList, this);

            mRecyclerView.setLayoutManager(mLayoutManager);
            mRecyclerView.setAdapter(mAdapter);

        }
    } 


    // the magic should happen here !
    @Override
    public void onItemClick(int position) {
        mfileList.get(position);
        getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container_primary, new Dashboardfragment()).commit();

        bottomNav.getMenu().getItem(1).setEnabled(true);
        //bottomNav.getMenu().getItem(2).setEnabled(true);

    }
}

The following lines does not work:

bottomNav.getMenu().getItem(1).setEnabled(true);

It seems to point to a null reference. I think it does not find the bottomNav define with R.id.bottom_navigation in the onCreateView method. In fact, the bottom_navigation is contained in the MainActivity layout, but that should not be a problem. I get this error:

    --------- beginning of crash
2020-04-15 20:09:20.202 8673-8673/com.flexicounts.flexicounts E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.flexicounts.flexicounts, PID: 8673
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Menu android.support.design.widget.BottomNavigationView.getMenu()' on a null object reference
        at com.flexicounts.flexicounts.Homefragment.onItemClick(Homefragment.java:145)
        at com.flexicounts.flexicounts.fileAdapter$fileViewHolder.onClick(fileAdapter.java:50)
        at android.view.View.performClick(View.java:5637)
        at android.view.View$PerformClick.run(View.java:22429)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

Solution

  • create a BottomNavigationView getBottomNav() method in main activity then get it from onItemClick

    in MainActivity:

    public BottomNavigationView getBottomNav(){ return findViewById(R.id.bottom_navigation); }
    

    in Homefragment:

    // the magic should happen here !
        @Override
        public void onItemClick(int position) {
            mfileList.get(position);
            getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container_primary, new Dashboardfragment()).commit();
    
            ((MainActivity) getActivity()).getBottomNav().getMenu().getItem(1).setEnabled(true);
            //bottomNav.getMenu().getItem(2).setEnabled(true);
    
        }