6

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.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Thomi
  • 194
  • 1
  • 2
  • 12

4 Answers4

3

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.

Prashant Kumar Sharma
  • 1,120
  • 1
  • 11
  • 21
3

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.

Zohra Khan
  • 5,182
  • 4
  • 25
  • 34
1

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..

0
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;
                    }
                });
Reena
  • 9
  • 1
  • From Review: Welcome to Stack Overflow! Please don't answer just with source code. Try to provide a nice description about how your solution works. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). Thanks – sɐunıɔןɐqɐp Oct 09 '18 at 06:31