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?
1 Answer 1
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.
-
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.user2873153– user28731532014年08月05日 22:37:59 +00:00Commented 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.griffinjm– griffinjm2014年08月05日 23:42:33 +00:00Commented Aug 5, 2014 at 23:42