thomi100
thomi100

Reputation: 304

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.

Upvotes: 7

Views: 11146

Answers (4)

Reena
Reena

Reputation: 9

bottomNavigationView.setOnNavigationItemSelectedListener
                (new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        Fragment selectedFragment = null;
                        switch (item.getItemId()) {
                            case R.id.action_item1:
                                selectedFragment = ItemOneFragment.newInstance();
                                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                                transaction.replace(R.id.frame_layout, selectedFragment);
                                transaction.commit();
                                // selectedFragment.getChildFragmentManager().beginTransaction();
                                break;

                            case R.id.action_item2:
                                selectedFragment = ItemTwoFragment.newInstance();
                                FragmentTransaction transactiona = getSupportFragmentManager().beginTransaction();
                                transactiona.replace(R.id.frame_layout, selectedFragment);
                                transactiona.commit();
                              //  selectedFragment = ItemThreeFragment.newInstance();
                                break;

                            case R.id.action_item3:
                            //  selectedFragment = ItemOneFragment.newInstance();
                                Intent intent=new Intent(MainView.this, YoutActivityLive.class);
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(intent);
                               // selectedFragment = ItemTwoFragment.newInstance();
                                break;


                            case R.id.action_item5:
                                selectedFragment = ItemOneFragment.newInstance();
                                FragmentTransaction transactionb = getSupportFragmentManager().beginTransaction();
                                transactionb.replace(R.id.frame_layout, selectedFragment);
                                transactionb.commit();
                               // selectedFragment = ItemFiveFragment.newInstance();
                                break;
                        }

                        return true;
                    }
                });

Upvotes: 0

Zohra Khan
Zohra Khan

Reputation: 5302

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.

Upvotes: 3

Harshit Bansal
Harshit Bansal

Reputation: 19

Bottom Navigation View is a navigation bar introduced in android library to make it easy to switch between views with a single tap. It can although be used for almost any purpose, but is most commonly used to switch between fragments with a single tap. Its use for opening activities is somewhat absurd, since it ignores its most important functionality of switching the views with a single tap. There are many good articles and blogs out there in this regard, one of which is: https://medium.com/@hitherejoe/exploring-the-android-design-support-library-bottom-navigation-drawer-548de699e8e0 Hope this solves your doubt..

Upvotes: 1

Prashant Kumar Sharma
Prashant Kumar Sharma

Reputation: 1118

Please refer to :-

https://github.com/waleedsarwar86/BottomNavigationDemo

and complete explanation in

http://waleedsarwar.com/posts/2016-05-21-three-tabs-bottom-navigation/

You will get a running code with the explanation here.

Upvotes: 2

Related Questions