Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch activity/fragment with bottom navigation

I created an activity with a bottom navigation bar. I googled a lot things about it but now I don't know how to handle this exactly. Before, I just started another activity when the user clicks the bottom navigation but I think it's not good.

How can I switch between the tabs? Do I have to work with fragments? And what about 'setContentView(int layoutResID)'? How can I do that? I'm confused...

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                return true;
            case R.id.navigation_dashboard:
                startActivity(dashboardActivity);
                return true;
            case R.id.navigation_notifications:
                startActivity(dashboardActivity);
                return true;
        }
        return false;
    }

};

Thank you very much for your help - I hope, you understood what I mean.

like image 518
Thomi Avatar asked Mar 09 '23 03:03

Thomi


1 Answers

Activity transition is always expensive and we should switch from one activity to another only when we are switching the context. A fragment is a portion of UI in an activity. Same fragment can be used with multiple activities. Just like activity a fragment has its own lifecycle and setContentView(int layoutResID) can be set to different layout in OnCreate of fragment.

This link explains more on when to use activity or fragment.

Android developer guide on Fragments

Code path tutorial on bottom navigation views.

like image 87
Zohra Khan Avatar answered Mar 19 '23 08:03

Zohra Khan