0

I am having quite a challenge, i was following some tutorial project but when we reached a point of adding a bottom navigation bar, I have been stuck here for weeks trying to figure out where the problem is. The activity loads successfully but i can not switch between the different tabs on the bottom navigation bar namely, Home, Add and Logout.

Here is my Code for the activity.

public class SellerHomeActivity extends AppCompatActivity {

private TextView mTextMessage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_seller_home);
    BottomNavigationView navView = findViewById(R.id.nav_view);
    navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()) {

                case R.id.navigation_home:
                    mTextMessage.setText(R.string.title_home);
                    return true;

                case R.id.navigation_add:
                    Intent intentCategory = new Intent(SellerHomeActivity.this, SellerProductCategoryActivity.class);
                    startActivity(intentCategory);
                    return true;

                case R.id.navigation_logout:
                    final FirebaseAuth mAuth;
                    mAuth = FirebaseAuth.getInstance();
                    mAuth.signOut();

                    Intent intentMain = new Intent(SellerHomeActivity.this, MainActivity.class);
                    intentMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intentMain);
                    finish();
                    return true;
            }

            return false;

        }
    });
    // Passing each menu ID as a set of Ids because each
    // menu should be considered as top level destinations.
    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
            R.id.navigation_home, R.id.navigation_add, R.id.navigation_logout)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);

// NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration); NavigationUI.setupWithNavController(navView, navController); }

}

Any help rendered will be so helpful to me, Thanks in advance

AllanTumu
  • 3
  • 4

3 Answers3

0

When you go to SellerProductCategoryActivity, you have no longer access to the switch statement where you are supposed to switch tabs because it is in SellerHomeActivity.

And also, you don't switch to any tabs here :

 case R.id.navigation_home:
                mTextMessage.setText(R.string.title_home);
                return true;

You just set a text to TextView but you actually should change to Home tab.

My suggestion would be, create an Activity for holding the switch statement. And convert your two activities which are SellerProductCategoryActivity and SellerHomeActivity to a Fragment. Inside your switch statement cases, just switch Fragments.

Moreover, it is not a good practice to have a BottomNavigationView item for log out functionality.

  • Yes i don't switch to any tabs in the 1st case and the logout button will further be removed as we proceed with the project. Is there any other approach you would suggest apart from the one suggested above? – AllanTumu May 28 '20 at 12:13
0

Another approach I can suggest making BottomNavigationView navView public static and adding another OnNavigationItemSelectedListener in your SellerProductCategoryActivity.

In this case first activity should look like this:

public class SellerHomeActivity extends AppCompatActivity {

public static TextView mTextMessage;
public static BottomNavigationView navView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seller_home);
navView = findViewById(R.id.nav_view);
navView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()) {

            case R.id.navigation_home:

                return true;

            case R.id.navigation_add:
                Intent intentCategory = new Intent(SellerHomeActivity.this, SellerProductCategoryActivity.class);
                startActivity(intentCategory);
                return true;

            case R.id.navigation_logout:
                final FirebaseAuth mAuth;
                mAuth = FirebaseAuth.getInstance();
                mAuth.signOut();

                Intent intentMain = new Intent(SellerHomeActivity.this, MainActivity.class);
                intentMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intentMain);
                finish();
                return true;
        }

        return false;

    }
});
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
        R.id.navigation_home, R.id.navigation_add, R.id.navigation_logout)
        .build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);

And the listener on SellerProductCategoryActivity should be like this:

SellerHomeActivity.navView.setOnNavigationItemSelectedListener(new 
BottomNavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()) {

            case R.id.navigation_home:
                SellerHomeActivity.mTextMessage.setText(R.string.title_home);
                Intent intentCategory = new Intent(SellerProductCategoryActivity.this, 
                SellerHomeActivity.class);
                startActivity(intentCategory);
                return true;

            case R.id.navigation_add:
                return true;

            case R.id.navigation_logout:
                final FirebaseAuth mAuth;
                mAuth = FirebaseAuth.getInstance();
                mAuth.signOut();

                Intent intentMain = new Intent(SellerHomeActivity.this, MainActivity.class);
                intentMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intentMain);
                finish();
                return true;
        }

        return false;

    }
});
0

First of all bottom navigation should work best with the fragments. try using fragments instead of activity and also I have made minor changes which don't really make a difference but I tried making the same pattern like how I have done it. so my code had fragments instead of activity and everything else is the same. I hope this should help you and it should work.

    public class SellerHomeActivity extends AppCompatActivity {

    private TextView mTextMessage;

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

        setContentView(R.layout.activity_seller_home);

        BottomNavigationView navView = findViewById(R.id.nav_view);

        navView.setOnNavigationItemSelectedListener(navListner);

        BottomNavigationView.OnNavigationItemSelectedListener navListner = new BottomNavigationView.OnNavigationItemSelectedListener() {

            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                switch (item.getItemId()) {

                    case R.id.navigation_home:
                        mTextMessage.setText(R.string.title_home);
                        break;

                    case R.id.navigation_add:
                        Intent intentCategory = new Intent(
                            SellerHomeActivity.this,
                            SellerProductCategoryActivity.class);
                        startActivity(intentCategory);
                        break;

                    case R.id.navigation_logout:
                        final FirebaseAuth mAuth;
                        mAuth = FirebaseAuth.getInstance();
                        mAuth.signOut();

                        Intent intentMain = new Intent(SellerHomeActivity.this, MainActivity.class);
                        intentMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intentMain);
                        finish();
                        break;
                }
                return true;
            }
        });

    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
            R.id.navigation_home, R.id.navigation_add, R.id.navigation_logout
        )
        .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
}
Nitin Tej
  • 353
  • 1
  • 7