moxy-community / Moxy

Moxy is MVP library for Android with incremental annotation processor and ktx features
MIT License
324 stars 33 forks source link

How to create a shared presenter between several fragments? #98

Open VashchenkoAndrey opened 4 years ago

VashchenkoAndrey commented 4 years ago

Hello. Could you help me please? I use Single Activity Architecture. I have a fragment with ViewPager. How to make the main fragment and view pager fragments served by one presenter? Thank you

aasitnikov commented 4 years ago

Hello.

There are several ways to tackle this problem.

If you have DI in your project, you can bind your main fragment's presenter to be a singleton in the scope of the main fragment, and then get it from that scope inside your pager fragments. An implementation may vary depending on the DI framework you use.

Another way I can think of is to crawl from viewpager fragments up the parent fragments until you find your main fragment and get presenter from the main fragment instance directly. You can refer to AndroidInjection and NavHostFragment for samples of parent fragment crawling.