DMCApps / NavigationFragment

The purpose of this manager is to work with the ViewPager, Tabs and Navigation drawer to handle a single stack flow of fragments on the screen. It makes use of a main Fragment as a container and presents and hides fragments within it as children.
MIT License
28 stars 8 forks source link

does it support switching between Fragment and NavigationFragment #3

Closed jjhesk closed 8 years ago

jjhesk commented 8 years ago

good work! There is an issue happened from switching between Fragment and NavigationFragment. I was running my code like so

 /**
     * require android-support-v4 import and the regular android fragment
     *
     * @param fragment    the unknown typed fragment
     * @param title       the string in title
     * @param oldFragment the previous fragment
     * @param closeDrawer if it needs to close the drawer after the new fragment has been rendered
     */
    public void setFragment(F fragment, String title, @Nullable F oldFragment, boolean closeDrawer) {
        currentFragmentNow = fragment;
        setTitle(title);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            // before honeycomb there is not android.app.Fragment
            android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            if (oldFragment != null && oldFragment != fragment)
                ft.remove((android.support.v4.app.Fragment) oldFragment);

            ft.replace(targetHomeFrame(), (android.support.v4.app.Fragment) fragment).commit();
        } else if (fragment instanceof android.app.Fragment) {
            if (oldFragment instanceof android.support.v4.app.Fragment)
                throw new RuntimeException("You should use only one type of Fragment");

            android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
            if (oldFragment != null && fragment != oldFragment)
                ft.remove((android.app.Fragment) oldFragment);

            ft.replace(targetHomeFrame(), (android.app.Fragment) fragment).commit();
        } else if (fragment instanceof android.support.v4.app.Fragment) {
            if (oldFragment instanceof android.app.Fragment)
                throw new RuntimeException("You should use only one type of Fragment");

            android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            if (oldFragment != null && oldFragment != fragment)
                ft.remove((android.support.v4.app.Fragment) oldFragment);
            ft.replace(targetHomeFrame(), (android.support.v4.app.Fragment) fragment).commit();
        } else
            throw new RuntimeException("Fragment must be android.app.Fragment or android.support.v4.app.Fragment");

        //if (closeDrawer)
        // getSlidingMenu().toggle(true);

        notifyOnBodyFragmentChange(currentFragmentNow);
    }

  public void initialNavigationFragmentManager(F firstFragment, @Nullable String title) {
        if (title != null) {
            setTitle(title);
        }
        if (firstFragment instanceof NavigationFragment) {
            SingleStackNavigationManagerFragment navManager = SingleStackNavigationManagerFragment.newInstance((NavigationFragment) firstFragment);
            mSingleStackNavigationManagerFragmentTag = UUID.randomUUID().toString();
            android.support.v4.app.FragmentTransaction fm = getSupportFragmentManager().beginTransaction();
            currentFragmentNow = firstFragment;
            fm.replace(targetHomeFrame(), navManager, mSingleStackNavigationManagerFragmentTag);
            fm.commit();
        } else if (firstFragment instanceof android.support.v4.app.Fragment) {
            setFragment(firstFragment, title);
        } else if (firstFragment instanceof Fragment) {
            setFragment(firstFragment, title);
        } else
            throw new RuntimeException("Fragment must be NavigationFragment");

    }

at the end it doesnt seems to support switching between fragment and the navigationfragment at the same framelayout.

DMCApps commented 8 years ago

Hey jjhesk,

Thanks a lot for all the feedback and the use of my library. Please note that my library relies on Fragments extending NavigationFragment (or NavigationListFragment) in order to be able to use the present and dismiss methods. However there should be no stopping you from using other fragment within containers.

I don't seem to have any issues with doing a remove/replace on my container. I just pushed some code that uses a NavigationDrawer as an example. In this code I use NavigationFragments and Fragments and it also uses replace/remove instead of attach/detach. Please take a look at the code in the branch git_issue_3 and let me know if you need some help from that.

If this doesn't help can you please elaborate on what the issue is exactly that you are facing? The point of this manager is to make an instance of one of the managers (and it's root or master-detail fragments), and then start presenting/dismissing fragments from within themselves. Please see my ReadMe and the SampleFragment for how to push/pop fragments on and off the stack.

jjhesk commented 8 years ago

please hold on this issue. I need to do some more extensive testing on my app to find out where its happening.

DMCApps commented 8 years ago

Alright will do. If there is anything I can do to help let me know. Here is the code snippet that I use for the add/replace/remove of the Normal Fragment/Navigation Fragment.

// START BLOCK onNavigationItemSelected
       if (id == R.id.nav_camera) {
// Using a NavigationFragment
            initialNavigationFragmentManager(SampleFragment.newInstance("Nav Camera", 0), "Camera");
        }
        else if (id == R.id.nav_gallery) {
// Using a support v4 Fragment
            initialNavigationFragmentManager(NonNavigationFragment.newInstance("Non-Nav Gallery"), "Gallery");
        }
        else if (id == R.id.nav_slideshow) {
// Using a NavigationFragment
            initialNavigationFragmentManager(SampleFragment.newInstance("Nav Slideshow", 0), "Slideshow");
        }
        else if (id == R.id.nav_manage) {
// Using a support v4 Fragment
            initialNavigationFragmentManager(NonNavigationFragment.newInstance("Non-Nav Manage"), "Gallery");
        }
// END BLOCK FROM onNavigationItemSelected

// How to use replace/add
    private int targetHomeFrame() {
        return R.id.frag_container;
    }

    private Fragment mVisibleFragment;

    /**
     * require android-support-v4 import and the regular android fragment
     *
     * @param fragment    the unknown typed fragment
     * @param title       the string in title
     * @param oldFragment the previous fragment
     * @param closeDrawer if it needs to close the drawer after the new fragment has been rendered
     */
    public void setFragment(Fragment fragment, String title, @Nullable Fragment oldFragment, boolean closeDrawer) {
        if (title != null) {
            setTitle(title);
        }

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (oldFragment != null && oldFragment != fragment)
            ft.remove(oldFragment);

        ft.replace(targetHomeFrame(), fragment).commit();
        mVisibleFragment = fragment;
    }

    private String mSingleStackNavigationManagerFragmentTag;

    public void initialNavigationFragmentManager(Fragment firstFragment, @Nullable String title) {

        if (firstFragment instanceof NavigationFragment) {
            SingleStackNavigationManagerFragment navManager = SingleStackNavigationManagerFragment.newInstance((INavigationFragment)firstFragment);
            setFragment(navManager, title, mVisibleFragment, false);
        }
        else if (firstFragment instanceof Fragment) {
            setFragment(firstFragment, title, mVisibleFragment, false);
        }
    }
jjhesk commented 8 years ago

thanks you! you have solved this problem handsomely. I have i think its a good idea to add this piece into your sample demonstration

DMCApps commented 8 years ago

Thanks jj,

I have merged in the NavigationDrawerExample into the main project.