ncapdevi / FragNav

An Android library for managing multiple stacks of fragments
1.5k stars 220 forks source link

FragNavController in Java #243

Open GoriunovAlexandr opened 3 years ago

GoriunovAlexandr commented 3 years ago

Could you please provide some implementation in java? with some initialisation for FragNavController and some onBackPressed method? In specialy I'm interested in back navigation. I can't implement a back navigation between root fragments. It pops up the fragment from current root, and doesn't move to prevoius root fragment. The app is simply closed. I am using implementation 'com.ncapdevi:frag-nav:3.2.0' version

@Override
    public int getNumberOfRootFragments() {
        return 5;
    }

  fragNavController = new FragNavController(getSupportFragmentManager(), R.id.container);
  fragNavController.setRootFragments(fragmentList);
  //fragNavController.setRootFragmentListener(this);  here it gives a mistake when the app is lauched:  IllegalStateException: 
        //Shouldn't have both a rootFragmentListener and rootFragments set, this is clearly a mistake
        //fragNavController.setCreateEager(true); if this line is used the library moves to the 3-rd root fragment
        fragNavController.setFragmentHideStrategy(FragNavController.DETACH_ON_NAVIGATE_HIDE_ON_SWITCH);
        fragNavController.setNavigationStrategy(new UniqueTabHistoryStrategy((int i, @Nullable FragNavTransactionOptions 
   fragNavTransactionOptions) -> {
            bottomNavigation.setSelectedItemId(i);
        }));
        fragNavController.initialize(FragNavController.TAB1, savedInstanceState);
        if (savedInstanceState == null) bottomNavigation.setSelectedItemId(FragNavController.TAB1);

   @Override
public void onBackPressed() {
    if (!fragNavController.isRootFragment()) {
        fragNavController.popFragment();
    } else {
        super.onBackPressed();
    }
}

Will be very glad for some help! Best regars, Alex