Search code examples
androidnavigationbarbottomnavigationviewonitemselectedlistener

OnNavigationItemsSelected Listener is starting the same activity again and again


I am new to Android Development And I am trying to create an application with bottomNavigationView. I have created a separate class to setup the bottomNavigationView for different activities so that I don't have to write the code again and Again. But when I launch the app in an Android device it is starting the Welcome activity again no matter what item I click it start Welcome activity. This is my navigationHelperClass

public class BottomNavigationViewHelper {
private static final String TAG = "BottomNavigationViewHel";

public static void setUpNavigationView(BottomNavigationViewEx bottomNavigationViewEx){
    Log.d(TAG, "setUpNavigationView: setting BottomNavigation");
    bottomNavigationViewEx.enableAnimation(false);
    bottomNavigationViewEx.enableItemShiftingMode(false);
    bottomNavigationViewEx.enableShiftingMode(false);
    bottomNavigationViewEx.setTextVisibility(false);

}
public static void enableNavigation(final Context context, final BottomNavigationViewEx viewEx){
    viewEx.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch(item.getItemId()){
                case R.id.btnHome:
                    viewEx.setSelectedItemId(R.id.btnHome);
                    Intent intent = new Intent(context, Welcome.class);
                    context.startActivity(intent);
                    break;
                case R.id.btnSearch:
                    Intent intent1 = new Intent(context, Chats.class);
                    context.startActivity(intent1);
                    break;
                case R.id.btnPost:
                    Intent intent2 = new Intent(context, Posts.class);
                    context.startActivity(intent2);
                    break;
                case R.id.btnFavourites:
                    Intent intent3 = new Intent(context, Favourites.class);
                    context.startActivity(intent3);
                    break;
                case R.id.btnProfile:
                    Intent intent4 = new Intent(context, Profile.class);
                    context.startActivity(intent4);
                    break;
            }
            return false;
        }
    });

}
}

This is my Welcome activity which starts when I click any of the item of bottomNavigationView.

public class Welcome extends AppCompatActivity { private Context mCntext = Welcome.this;

private static final String TAG = "Welcome";
BottomNavigationViewEx bottomNav;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_welcome);
    Log.d(TAG, "onCreate: starting");
    setupBottomNavigationView();

    Menu menu = bottomNav.getMenu();
    MenuItem menuItem = menu.getItem(0);
    menuItem.setChecked(true);

private void setupBottomNavigationView(){
    Log.d(TAG, "setupBottomNavigationView: setting bottomnavigationview");
    bottomNav = findViewById(R.id.nav_bottom);
    BottomNavigationViewHelper.setUpNavigationView(bottomNav);
    BottomNavigationViewHelper.enableNavigation(mCntext, bottomNav);
}
}

This is one of the activities that I have and the code is same for the rest of activities too . This is Profile activitity.

public class Profile extends AppCompatActivity {

private Context mContext = Profile.this;
private static final String TAG = "Search";
BottomNavigationViewEx bottomNav;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    setupBottomNavigationView();

}

private void setupBottomNavigationView(){
    Log.d(TAG, "setupBottomNavigationView: setting bottomnavigationview");
    bottomNav = findViewById(R.id.nav_bottom);
    BottomNavigationViewHelper.setUpNavigationView(bottomNav);
    BottomNavigationViewHelper.enableNavigation(mContext, bottomNav);
    Menu menu = bottomNav.getMenu();
    MenuItem menuItem = menu.getItem(4);
    menuItem.setChecked(true);
}
@Override
public void setTitle(CharSequence title) {
}
}

Solution

  • Remove following line:

    viewEx.setSelectedItemId(R.id.btnHome);

    from case case R.id.btnHome: under onNavigationItemSelected callback.