Giter VIP home page Giter VIP logo

Comments (9)

aartikov avatar aartikov commented on May 27, 2024 1

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.

mjurkus avatar mjurkus commented on May 27, 2024

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.

aartikov avatar aartikov commented on May 27, 2024

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.

mjurkus avatar mjurkus commented on May 27, 2024

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

Fragment previousFragment = fragments.get(fragments.size() - 2);

it could be passed here
navigationContext.getScreenResultHelper().callScreenResultListener(currentFragment, mScreenResult, navigationContext.getScreenResultListener(), navigationFactory);

Although I haven't read whole code, there might be some issues with this approach.

from alligator.

aartikov avatar aartikov commented on May 27, 2024

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.

mjurkus avatar mjurkus commented on May 27, 2024

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.

aartikov avatar aartikov commented on May 27, 2024

So, what do you suggest?

from alligator.

mjurkus avatar mjurkus commented on May 27, 2024

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.

aartikov avatar aartikov commented on May 27, 2024

More options - more possibilities for bugs :)

And don't forget about other cases:

  1. Starting activity for result and handling result on a fragment.
  2. Starting activity for result and handling result on a page of a screen switcher.

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.