Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have written a rather small App with only one activity. From my understanding an activity has to be seen as kind of a view and controller in one class.

But even for a little app, with 3 dialogs (made with AlertDialogBuilder and all those anonymous inner classes that come with it) my activity has 500+ LOC and became very hard to maintain. The activity houses mainly android lifecycle handlers and view handling. Business logic is very little.

How to handle activity classes to keep them readable and maintainable?

share|improve this question
1  
You should have a look at [AndroidAnnotations][github.com/excilys/androidannotations]. It helps remove big part of the plumbing code –  RMalke Aug 7 '13 at 14:10
1  
The link should be AndroidAnnotations –  k3b Aug 7 '13 at 14:24
add comment

2 Answers

When I develop for Android, I always think of my Activities as my controllers and my layouts (XML) as my views. The layouts interact with each other and the business logic through my Activities (controllers). Here are some mistakes I learned a lot from:

  • Keep your component Listeners as separate classes. I used to keep them as inner classes to my components (mainly Buttons). This made my Activity code a pure mess and editing it was like hell.
  • Try and have separate Activities for every action you have. This will make your code easier to get back to if you need to change stuff (and thus more maintainable)
share|improve this answer
    
If the listeners are separate classes I do not have access to the activities members, how do you handle this? Usually you would need access to those members from e.g. a click handler. –  metacircle Apr 20 '13 at 5:30
add comment

Below are the points you need to remember:

  • Always bifurcate your codes in different methods. I mean for example, if you are registering the various event listeners then make one method "registerListeners()"

    public void registerListeners() {
         // your button, listview....components event listeners codes
    }
    

    then in onCreate(), call this method. By doing this, you have all registered listener codes at one place and they more readable and searchable.

  • getting all controls ID will be in another method named "initControls"

    public void initControls() {
         buttonSave=(Button)findviewById(R.id.save);
         listviewItem=(ListView)findviewById(R.id.listviewItem);
         // etc..etc...components
    }
    

    then in onCreate(), call this method. By doing this, you will have all controls id at one place and it will look very neat and clean. you can search it very easily.

    onCreate() {
         //after setcontentview
         initControls();
         registerListeners();
    }
    

Hope, this helps you to explore more on code writing. :)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.