2

I'm making an Android app and have it structured as a simple multi-activity app. I want to add a navigation drawer to it, and it looks like I will need to completely restructure the app and load everything as fragments. (http://developer.android.com/training/implementing-navigation/nav-drawer.html)

How should I begin performing this "movement" of code? Should I completely copy and paste my class variables and functions into a giant file? This definitely feels like it would be prone to bugs and errors. Perhaps there's a method of separating the class files but still having one navigation drawer?

asked Aug 4, 2014 at 23:19

1 Answer 1

0

I have recently had to do something similar with a relatively complex app (lots of NFC + networking + custom views). I found that I ended up with a massive ~1500 line main class.

So I split up the various functional parts of the app into several classes. All of these subclass a manager superclass which I defined as having only one method: attach(). It takes an activity as an argument and stores it as a local variable. This allows the manager classes to have a reference to a context.

So now I have several manager classes all instantiated in my Main onCreate. UIManager, NfcManager, NetworkManager, etc. Each is responsible for its respective logic and is easy to modify individually without having to modify the main activity class.

answered Aug 5, 2014 at 18:50
2
  • Yeah, I'm essentially converting all my Activity files into Fragment files now, so I no longer need to merge everything into a giant massive class. It's a bit of a pain reworking the boilerplate code, but I assume that this is unavoidable. Commented Aug 5, 2014 at 22:37
  • I've tried to keep as much logic as possible outside of my fragments and have them just doing the minimum. I've tried to build a decoupled app with the main activity basically just handling the lifecycle callbacks and being used by the previously mentioned Manager classes to call various system methods. I've moved all my networking into an IntentService and communication is done between the Fragments. Activity, and IntentService by using LocalBroadcastManager and BroadcastReceivers. Although I've recently discovered Otto square.github.io/otto which I think could be very useful. Commented Aug 5, 2014 at 23:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.