Giter Club home page Giter Club logo

Comments (2)

ncapdevi avatar ncapdevi commented on May 18, 2024

Hey there, so if I'm reading this correctly, it looks like what you're trying to do is, when back press happens, if you're on the fragment that has a drawer, and if that drawer is open, close it, then on the next back press, perform actions as normal, is that correct? If so, this code looks correct to me, although I would add one small addition


@Override
    public void onBackPressed() {
        if (mCurrentIndex == INDEX_RECENTS && mDrawer != null && mDrawer.isDrawerOpen()) {
            mDrawer.closeDrawer();
        } else if (mNavController.getCurrentStack().size() > 1) {
            mNavController.popFragment();
        } else {
            super.onBackPressed();
        }
    }
@Override
   public Fragment getRootFragment(int i) {
    mCurrentIndex = i;
       switch (i) {
           case INDEX_RECENTS:
               return ExploreBaseFragment.newInstance(0); //this is fragment with BottomNavigationView having 3 child fragment
           case INDEX_FAVORITES:
               return MyUploadsFragment.newInstance(0);
           case INDEX_NEARBY:
               return CategoryFragment.newInstance(0);
       }
       throw new IllegalStateException("Need to send an index that we know");
   }

I should say that having to create this mCurrentIndex is a hassle, so in the upcoming version of the library (version 2.0.0) there is a helper function mNavController.getCurrentStackIndex() that will make this easier, but there's still a tiny bit of work to be done on that.

If this isn't working, could you tell me more of what seems to be happening?

from fragnav.

yogirana5557 avatar yogirana5557 commented on May 18, 2024

Sorry but I don't have any problem with mDrawer.closeDrawer();. In my Navigation activity I am switching fragments using mNavController.switchTab(INDEX_NEARBY); and in onBackpressed() handling Back using

 if (mNavController.getCurrentStack().size() > 1) {
            mNavController.popFragment();
        }

But one of these fragment I have three child fragments so how do I add those fragments in mNavController and handle their Back as they are in fragment. I hope you understood my problem.

public class MainActivity extends AppCompatActivity implements Drawer.OnDrawerItemClickListener, BaseFragment.FragmentNavigation, FragNavController.RootFragmentListener {

    private static final String TAG = "MainActivity";
    private final int INDEX_RECENTS = FragNavController.TAB1;
    private final int INDEX_FAVORITES = FragNavController.TAB2;
    private final int INDEX_NEARBY = FragNavController.TAB3;
    //save our header or mDrawer
    private AccountHeader mAccountHeader = null;
    private Drawer mDrawer = null;
    private FragNavController mNavController;

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

        // set Toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //create account header
        createAccountHeader(savedInstanceState);

        //Create the drawer
        createDrawer(savedInstanceState, toolbar, getDrawerItems());

        mNavController = new FragNavController(savedInstanceState, getSupportFragmentManager(), R.id.frame_container, this, 3, INDEX_RECENTS);

    }

    @Override
    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
        Log.d(TAG, "onItemClick Position: " + position);
        if (drawerItem != null) {
            if (drawerItem.getIdentifier() == 100) {
                mNavController.switchTab(INDEX_RECENTS);
            } else if (drawerItem.getIdentifier() == 200) {
                mNavController.switchTab(INDEX_FAVORITES);
            } else if (drawerItem.getIdentifier() >= 0 && drawerItem.getIdentifier() <= 15) {
                mNavController.switchTab(INDEX_NEARBY);
            } else if (drawerItem.getIdentifier() == 500) {
                mNavController.showDialogFragment(new RatingFragment());
            } else if (drawerItem.getIdentifier() == 700) {
                mNavController.showDialogFragment(new SignOutFragment());
            } else if (drawerItem.getIdentifier() == 400) {
                startActivity(new Intent(MainActivity.this, SettingsActivity.class));
            }
        }
        return false;
    }

    @Override
    public void onBackPressed() {
        if (mDrawer != null && mDrawer.isDrawerOpen()) {
            mDrawer.closeDrawer();
        } else if (mNavController.getCurrentStack().size() > 1) {
            mNavController.popFragment();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void pushFragment(Fragment fragment) {
        mNavController.pushFragment(fragment);
    }

    @Override
    public Fragment getRootFragment(int i) {
        switch (i) {
            case INDEX_RECENTS:
                return ExploreBaseFragment.newInstance(0); //Fragment with three child fragment
            case INDEX_FAVORITES:
                return MyUploadsFragment.newInstance(0);
            case INDEX_NEARBY:
                return CategoryFragment.newInstance(0);
        }
        throw new IllegalStateException("Need to send an index that we know");
    }
}

This is fragment with three child fragments

public class ExploreBaseFragment extends BaseFragment implements BottomNavigationView.OnNavigationItemSelectedListener, FragNavController.RootFragmentListener {

    private final int INDEX_POPULAR = FragNavController.TAB1;
    private final int INDEX_EXPLORE = FragNavController.TAB2;
    private final int INDEX_FAVORITES = FragNavController.TAB3;
    private FragNavController mNavController;

    public static ExploreBaseFragment newInstance(int instance) {
        Bundle args = new Bundle();
        args.putInt(ARGS_INSTANCE, instance);
        ExploreBaseFragment fragment = new ExploreBaseFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_explore_base, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        BottomNavigationView bottomNavigationView = (BottomNavigationView) view.findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(this);
        final MenuItem menuItem = bottomNavigationView.getMenu().findItem(R.id.action_explore);
        menuItem.setChecked(true);

        mNavController = new FragNavController(savedInstanceState, getChildFragmentManager(), R.id.frame_container, this, 3, INDEX_POPULAR);

        onNavigationItemSelected(menuItem);

    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_popular:
                mNavController.switchTab(INDEX_POPULAR);
                break;
            case R.id.action_explore:
                mNavController.switchTab(INDEX_EXPLORE);
                break;
            case R.id.action_favourites:
                mNavController.switchTab(INDEX_FAVORITES);
                break;
        }
        return true;
    }

    @Override
    public Fragment getRootFragment(int i) {
        switch (i) {
            case INDEX_POPULAR:
                return PopularFragment.newInstance(0);
            case INDEX_EXPLORE:
                return ExploreFragment.newInstance(0);
            case INDEX_FAVORITES:
                return FavoritesFragment.newInstance(0);
        }
        throw new IllegalStateException("Need to send an index that we know");
    }
}

I don't know how to use/handle FragNavController in above fragment. So any suggestions on that?
And also as seen in Example I am also using BaseFragment having FragmentNavigation interface but void pushFragment(Fragment fragment) this method is nowhere use .

from fragnav.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.