Correct Architecture for Bottom Navigation Bar Android Application

Viewed 245

I would like to get some input on the best way to structure my app's architecture when using Android's Bottom Navigation View.

Currently I define my BottomNavigationView in my MainActivity. It looks something like this.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment selectedFragment = null;

                switch (item.getItemId()){
                    case R.id.action_home:
                        selectedFragment = HomeFragment.newInstance();
                        break;

                    case R.id.action_search:
                        selectedFragment = SearchFragment.newInstance();
                        break;

                    case R.id.action_message:
                        selectedFragment = MessageFragment.newInstance();
                        break;

                    case R.id.action_profile:
                        selectedFragment = ProfileFragment.newInstance();
                        break;
                }

                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.frame_layout, selectedFragment);
                transaction.commit();
                return true;
            }
        });

        //Manually displaying the first fragment - one time only
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.frame_layout, HomeFragment.newInstance());
        transaction.commit();

    }

The problem is that once I click on one tab, it opens up a fragment, and I would like to have those Fragments open up other Fragments/Activities (i.e:

  1. I open the profile tab (`ProfileFragment` loads)
  2. I click on a button from `ProfileFragment`, and from this the `SignUpFragment` loads

After running into many bugs, I've researched on how to architect my app, but i've found mixed results. Would anyone know the correct way of using a BottomNavigationView with Fragments, and in those fragments I can load more Activities/fragments. A huge thanks in advance.

0 Answers
Related