Comments (3)
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.
Surely. You can implement your own ScreenSwitcher
based on a view pager.
from alligator.
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)
- Question: How to get the activity result from a fragment? HOT 1
- Handle result if activity is destroyed HOT 10
- Nested fragment ScreenSwitcher HOT 1
- Integration with AAC Navigator HOT 1
- Fragment BackStack Handling HOT 4
- Navigation tree HOT 2
- In which case getScreen can return null ? HOT 5
- Kotlin HOT 2
- Translucent activity not finished with result HOT 4
- It makes sense to have a method returning the current screen (and maybe the activity/fragment) on AndroidNavigator? HOT 1
- AndroidX HOT 3
- Can an Instagram Like navigation be done with this Library HOT 2
- Cannot generate Class HOT 1
- Help Needed HOT 8
- screenResolver.getScreen() returns null HOT 4
- [Improvement] Make a new navigator method to keep already loaded Fragments. HOT 1
- Pop fragments/screens if HOT 2
- Passing ScreenResult from one fragment to another fragment HOT 9
- "Race condition" when using switchTo() HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from alligator.