Giter VIP home page Giter VIP logo

Comments (3)

aartikov avatar aartikov commented on May 26, 2024 1

The solution I'm thinking of is to get the fragments from the NavigationFactory when the FragmentPagerAdapter asks for them.

Yes, it should work.

but I don't know if then the fragments will be always kept on memory even if they are several pages.

NavigationFactory just creates fragments and does not hold references to them anymore. Then FragmentPagerAdapter will handle this fragments as it's supposed to do. According to the documentation FragmentPagerAdapter keeps all created fragments in the fragment manager. It is okay if you have a small amount of pages. In other case you can try FragmentStatePagerAdapter.

from alligator.

aartikov avatar aartikov commented on May 26, 2024

Surely. You can implement your own ScreenSwitcher based on a view pager.

from alligator.

emanzanoaxa avatar emanzanoaxa commented on May 26, 2024

Can you give me some hints? I've been checking FragmentScreenSwitcher and I can't realize how to mix it with a FragmentPagerAdapter.
The solution I'm thinking of is to get the fragments from the NavigationFactory when the FragmentPagerAdapter asks for them, but I don't know if then the fragments will be always kept on memory even if they are several pages.

EDIT: this seems to do the work

/**
 * Fragment state pager adapter that gets the fragments from NavigationFactory, the fragments
 * to be loaded are defined in a Screen list.
 */
public class ScreenFragmentPagerAdater extends FragmentPagerAdapter {

    protected final List<Screen> screens;
    private final NavigationFactory navigationFactory;
    private final Map<Screen, WeakReference<Fragment>> fragmentMap;

    public ScreenFragmentPagerAdater(FragmentManager fm, NavigationFactory navigationFactory) {
        super(fm);
        Preconditions.checkNotNull(navigationFactory);
        this.navigationFactory = navigationFactory;
        fragmentMap = new HashMap<>();
        this.screens = new ArrayList<>();
    }

    public void setScreens(@NonNull List<Screen> screens) {
        Preconditions.checkNotNull(screens);
        this.screens.clear();
        this.screens.addAll(screens);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        fragmentMap.put(screens.get(position), new WeakReference<>(fragment));
        return fragment;
    }

    @Override
    public Fragment getItem(int position) {
        try {
            return getOrCreateFragment(screens.get(position));
        } catch (ScreenSwitchingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int getCount() {
        return screens.size();
    }

    private Fragment getOrCreateFragment(Screen screen) throws ScreenSwitchingException {
        Fragment fragment = fragmentMap.get(screen) != null ? fragmentMap.get(screen).get() : null;
        if (fragment == null) {
            fragment = navigationFactory.createFragment(screen);
            try {
                navigationFactory.getScreen(fragment);  // Check that the screen has a valid screen getting function
            } catch (Exception e) {
                throw new ScreenSwitchingException(e.getMessage());
            }
            fragmentMap.put(screen, new WeakReference<>(fragment));
        }
        return fragment;
    }

}

from alligator.

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.