Bottom Navigation Menu onClick icon select

Edward Brand picture Edward Brand · Jan 17, 2019 · Viewed 10.5k times · Source

Two problems which I think are related. Background: I have three bottom navigation items which lead to other layouts and activities; one of which is Home.

  1. From Home, when onClick is performed, selected activity and layout opens up, yet the selected icon only highlights on click and then highlights Home. If pressed again, then it highlights selected layout icon. How do I make the icon highlighted on first click?

  2. Home has three other buttons that lead to other activities. When Home is selected, it takes considerably long time to return Home. Why is that so? Other activities/layouts are empty for now. Do I need to run a service in the background? Home Java/Activity code isn't that long, yet it takes a long time to return to.

Would really appreciate some thoughts. Maybe the answer is in fragments, not sure if that will solve the first problem though? I have included my code for bottom navigation from Home. Regards, Edward

P.S. I have only started working with Java and Android Studio a month ago.

    //Declare navigation view ID (bottomnav_view) in content_main
    BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomnav_view);
    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.ic_home: break;
                case R.id.ic_instruments:
                    Intent intentinstruments = new Intent(MainActivity.this, InstrumentListActivity.class);
                    startActivity(intentinstruments);
                    break;
                case R.id.ic_methods:
                    Intent intentmethods = new Intent(MainActivity.this, MethodsActivity.class);
                    startActivity(intentmethods);
                    break;
            }
            return true;
        }
    });
    //End of bottom hav handler

Answer

Rajat Mittal picture Rajat Mittal · Jan 17, 2019

Fragments are always preferred while implementing BottomNavigationView.

Try this way.

bottomNavigationView.setOnNavigationItemSelectedListener(new  
    BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment selectedFragment = null;

        switch (item.getItemId()){
            case R.id.navigation_News:
                selectedFragment = ItemoneFragment.newInstance();
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.content,selectedFragment);
                transaction.addToBackStack(null);
                transaction.commit();
                return true;

            case R.id.navigation_profile:
                selectedFragment = ItemtwoFragment.newInstance();
                transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.content,selectedFragment);
                transaction.addToBackStack(null);
                transaction.commit();
                return true;

        }
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.content, selectedFragment);
        transaction.commit();
        return true;
    }
});