0

Is there an (easy) way to wrap the navigation drawer view (more precisely android.support.design.widget.NavigationView) from the design support library in a Fragment?

The motivation behind this is to move navigation drawer related code (e.g. a toggle that triggers a network request) out of the hosting Activity into a separate module.

I'm looking for reference implementations or documents that describe how to implement such a behavior.

Taig
  • 6,718
  • 4
  • 44
  • 65

1 Answers1

0

You can use MVP pattern for this.

View in your case - it's NavigationDraver
Presenter - it's a simple interface which declared user iteractions.

For ex.

public interface DrawerPresenter {

    void toggleNetwork();

    void goOffline();

    void showSettings();
}

Actually, you just rebroadcasted all user actions to Presenter. After this you can move DrawerPresenter and Activity logic to different modules.

Profit in this case :

  • more clear logic
  • agile architecture
  • independence from UI
Sergey Shustikov
  • 15,377
  • 12
  • 67
  • 119
  • I don't see what I can gain by this approach. At the end of the day I still have to spawn a retained Fragment from my Activity in order to properly handle lifecycles. This could be easily avoided if the drawer itself was a Fragment. Correct me if I'm wrong, maybe I'm just not seeing the big picture here, since I never worked with this pattern. – Taig Dec 08 '15 at 00:54