简体   繁体   中英

How does 'default' bottom navigation view changes fragments?

I've seen many articles how to implement bottom navigation view, but none of those solutions looked like one from new project created with Bottom Navigation Activity:

BottomNavigationView navView = findViewById(R.id.nav_view);
AppBarConfiguration.Builder(
    R.id.navigation_timer, R.id.navigation_presets)
    .build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);

There are no transactions, no FragmentManager etc.

Reason I'm asking is because I'm trying to implement button on Fragment2 (PresetsFragment), which after click sets values and switches view to Fragment1 (TimerFragment). This is implementation of interface in MainActivity:

@Override
public void updateCountdowns(final int round_time, final int break_time)
{
    TimerFragment timer = (TimerFragment)getSupportFragmentManager().findFragmentById(R.id.navigation_timer);
    if (timer != null) {
        timer.updateCountdowns(round_time, break_time);
    } else
    {
        TimerFragment new_timer = new TimerFragment();
        Bundle args = new Bundle();
        args.putInt("round_time_bundle", round_time);
        args.putInt("break_time_bundle", break_time);
        new_timer.setArguments(args);
        FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction()
                .replace(R.id.navigation_presets, new_timer, "new_timer")
                .addToBackStack(null)
                .commit();
    }
}

After adding logs I can see that interface works and new `TimerFragment' is created - but fragment itself doesn't change.

Actually comment of John Joe helped me a lot. There is no reason to use interface between fragments if I use Navigation .

Bundle args = new Bundle();
args.putInt("round_time_bundle", round_time);
args.putInt("break_time_bundle", break_time);
Navigation.findNavController(view).navigate(R.id.navigation_timer, args);

In my example this is all that is needed. Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM