ZWis212
ZWis212

Reputation: 132

Navigation Drawer Fragments

So I have my navigation drawer with 5 different options. They all open a new fragment that I have created. The first one is Home, and I am trying to find a way to bring it back to the first screen that shows up under the navigation drawer. It has the id of "container", in the main_activity.xml. I do not want to use and intent to call the entire class again to load up. Also I do not want to be able to use the back button from another intent. I am confused on how to make this happen.

 @Override
public void onNavigationDrawerItemSelected(int position) {

    FragmentHowItWorks fragmentHow;
    FragmentSettings fragmentSettings;
    FragmentTransaction transaction = getFragmentManager().beginTransaction();

    switch(position){
        case 0:

                // should I call the layout?
                // this is the "Home" option

            break;
        case 1:
            fragmentHow = new FragmentHowItWorks();
            transaction.replace(R.id.container, fragmentHow);
            transaction.addToBackStack(null);
            transaction.commit();
            break;
        case 2:
            fragmentSettings = new FragmentSettings();
            transaction.replace(R.id.container, fragmentSettings);
            transaction.addToBackStack(null);
            transaction.commit();
            break
        case 3:
            fragment = new FragmentHowItWorks();
            transaction.replace(R.id.container, fragment);
            transaction.addToBackStack(null);
            transaction.commit();
              break;
        case 4:
              fragment = new FragmentHowItWorks();
              transaction.replace(R.id.container, fragment);
              transaction.addToBackStack(null);
              transaction.commit();
              break;
    }
}

Upvotes: 0

Views: 188

Answers (3)

jmsalcido
jmsalcido

Reputation: 1077

I know that this was asked and answered long time ago but, I would like to show my approach on this problem, maybe it will help anyone else:

I added a Fragment Name over each Fragment that I use like:

public class MainFragment extends BaseFragment {
    public static final String FRAGMENT_TAG = "main";
    // ... all your fragment
}

And on the Drawer Layout:

public void methodThatSetsTheUi() {
     mDrawerView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            getFragmentManager().findFragmentByTag("main");
            String tag = getFragmentTag(position);
            replaceFragment(getOrCreateFragment(tag), tag);
            mDrawerLayout.closeDrawer(mDrawerView);
        }
    });
}

@NonNull
private String getFragmentTag(int position) {
    String tag;
    switch (position) {
        case MAIN_FRAGMENT_DRAWER_POSITION:
            tag = MainFragment.FRAGMENT_TAG;
            break;
        case FAVORITE_FRAGMENT_DRAWER_POSITION:
            tag = FavoriteFragment.FRAGMENT_TAG;
            break;
        default:
            throw new AssertionError("That selection is wrong");
    }
    return tag;
}

private BaseFragment getOrCreateFragment(String fragmentName) {
    BaseFragment fragment = (BaseFragment) getFragmentManager().findFragmentByTag(fragmentName);
    if(fragment == null) {
        fragment = FragmentFactory.createFragment(fragmentName);
    }
    return  fragment;
}

and well, the FragmentFactory is just a simple Factory:

public final class FragmentFactory {

public static BaseFragment createFragment(String fragmentName) {
    switch(fragmentName) {
        case MainFragment.FRAGMENT_TAG:
            return MainFragment.newInstance();
        case FavoriteFragment.FRAGMENT_TAG:
            return FavoriteFragment.newInstance();
        // ... all fragments here.
        default:
            return null;
    }
}

Hope to help someone.

Upvotes: 0

yummy
yummy

Reputation: 580

Use methods add,hide and show,

Step1 Prepare all your fragments

Fragment fragment1 = new FragmentOne();
Fragment fragment2 = new FragmentTwo();
Fragment fragment3 = new FragmentThree();
Fragment mCurrentFragment = null;

Step2 Show/hide your fragments

@Override
public void onNavigationDrawerItemSelected(int position) {
    Fragment fragment;
    switch (position) {
    case 1:
        fragment = fragment1;
        break;
    case 2:
        fragment = fragment2;
        break;
    case 3:
        fragment = fragment3;
        break;
    }
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    if(mCurrentFragment == null) {
        ft.add(R.id.container, fragment).commit();
        mCurrentFragment = fragment;
    } else if(fragment.isAdded()) {
        ft.hide(mCurrentFragment).show(fragment).commit();
    } else {
        ft.hide(mCurrentFragment).add(R.id.container, fragment).commit();
    }
    mCurrentFragment = fragment;
}

Upvotes: 3

Heshan Sandeepa
Heshan Sandeepa

Reputation: 3687

You can do this ,

  1. You can get the name of current fragment which is in the container . This will return name including the package + fragment name

    String name = getFragmentManager().findFragmentById(container id ).getClass().getName();
    
  2. When you click on the home index of drawer, check weather the current name id equal to the home.

  3. If it is home, you don't need to perform any action.

Upvotes: 0

Related Questions