Comments (9)
You can implement screen result listener like that to handle screen result in fragments:
@Override
public void onScreenResult(Class<? extends Screen> screenClass, @Nullable ScreenResult result) {
FragmentManager fragmentManager = activity.getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentById(containerId);
if (fragment instanceof ScreenResultListener) {
((ScreenResultListener) fragment).onScreenResult(screenClass, result);
}
}
The library does not use setTargetFragment.
from alligator.
Yeah, that would work.
Although it would be nice if I could avoid accessing FragmentManager
altogether and register listener when fragment is added to backstack.
That would require support of multiple listeners of course.
from alligator.
Activities are responsible for creating and binding of NavigationContext
, so it is not quite clear how to configure it from fragments. Anyway you can try to implement some kind of composite listener to have multiple listeners in one.
Also look at my comments to this rejected pull request #4
It has something common with the issue.
from alligator.
With composite listener it's same story - it's error prone. I would have to manage such listeners.
onResume
- register listener in CompositeResultListener
, on destroy - unregister.
On the other hand if this fragment implements ScreenResultListener
it could be passed here
Although I haven't read whole code, there might be some issues with this approach.
from alligator.
Yes, it is possible to check if a previous fragment implements ScreenResultListener
and call it in BackCommand
. Similar change can be done for activitities.
But in my opinion that api would be less flexible and not consistent. Now there are no requirement that an activity or a fragment have to be a listener. An inner class can be a listener for example. Also the api is consistent now. Screen result listener is set in the same manner as other listeners are done: TransitionListener, NavigationErrorListener and other.
It requires some additional code to establish result handing in fragments. But it can be done once in BaseActivity.
from alligator.
Yes, it is consistent, but not flexible and requires accessing fragment stack to implement behaviours similar to mine or mentioned in #4, which defeats the purpose of the api.
from alligator.
So, what do you suggest?
from alligator.
I understand that implicit result returning to fragment implementing ScreenResultListener
isn't the best solution.
To mitigate that there could be option in NavigationContext.Builder
to toggle such behaviour - attemptResultDeliveryToFragment
or whatever :) Naming is not my strong suite.
If fragment consumed the result - returns true
and result propagation stops. If false
try to deliver to activity/default listener.
That way it gives control to user, keeps some of the consistency - behaviour is controlled.
What do you think?
from alligator.
More options - more possibilities for bugs :)
And don't forget about other cases:
- Starting activity for result and handling result on a fragment.
- Starting activity for result and handling result on a page of a screen switcher.
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
- Fragments and ViewPager HOT 3
- "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.