I'm using a BottomNavigationView in my project and when I press an item and load e.g. My Ticket fragment, which generates a QR-code and takes a while, there is a huge delay until the item is selected. I tried to put an AsyncTask to load all data in onResume() and onActivityCreated() in the Fragment but it didn't work well.
How can I use Bottom Navigation View and Fragments smoothly?
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation_view);
Menu test = bottomNavigationView.getMenu();
test.add(Menu.NONE, 1, Menu.NONE, "Home").setIcon(R.drawable.ic_action_home);
test.add(Menu.NONE, 2, Menu.NONE, "Agenda").setIcon(R.drawable.ic_action_event);
test.add(Menu.NONE, 3, Menu.NONE, "Ticket").setIcon(R.drawable.ic_action_credit_card);
test.add(Menu.NONE, 4, Menu.NONE, "Profile").setIcon(R.drawable.ic_action_person);
test.add(Menu.NONE, 5, Menu.NONE, "More").setIcon(R.drawable.ic_action_more);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectedFragment = null;
switch (item.getItemId()) {
case 1:
if(menuFrame.getVisibility() == View.VISIBLE) {
closeBottomMenu();
}
selectedFragment = HomeFragment.newInstance();
break;
case 2:
if(menuFrame.getVisibility() == View.VISIBLE) {
closeBottomMenu();
}
selectedFragment = AgendaFragment.newInstance();
break;
case 3:
if(menuFrame.getVisibility() == View.VISIBLE) {
closeBottomMenu();
}
selectedFragment = TicketFragment.newInstance();
break;
case 4:
if(menuFrame.getVisibility() == View.VISIBLE) {
closeBottomMenu();
}
selectedFragment = ProfileFragment.newInstance();
break;
case 5:
if(menuFrame.getVisibility() == View.INVISIBLE) {
openBottomMenu();
} else {
closeBottomMenu();
}
break;
}
if(selectedFragment != null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_main, selectedFragment);
transaction.commit();
}
return true;
}
});