Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle backstack using Navigation Activity and BottomNavigationView at same time #56

Closed
yogirana5557 opened this issue Mar 27, 2017 · 2 comments
Assignees
Labels

Comments

@yogirana5557
Copy link

Hello I am using this Material Drawer library to create drawer & I have BottomNavigationView in first fragment of drawer.
So I am using this to handle back but not able to maintain back it with BottomNavigationView child fragment. Please help me on this

@Override
    public void onBackPressed() {
        if (mDrawer != null && mDrawer.isDrawerOpen()) {
            mDrawer.closeDrawer();
        } else if (mNavController.getCurrentStack().size() > 1) {
            mNavController.popFragment();
        } else {
            super.onBackPressed();
        }
    }
@Override
   public Fragment getRootFragment(int 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");
   }
@ncapdevi ncapdevi self-assigned this Mar 28, 2017
@ncapdevi
Copy link
Owner

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?

@yogirana5557
Copy link
Author

yogirana5557 commented Mar 28, 2017

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 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants