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 am starting a new Android application. What is the best structure to use?

I am planning to make it a multi-package design as follows:

  1. Main package, including the Activity
  2. Service and data layer
  3. Entity package, including entity class.

    Any advice ?

share|improve this question
add comment

3 Answers

up vote 8 down vote accepted

First, it depends on what application you are doing.

you should do a textual or schematic description on how a user will work with application. Fix down every possible scenario. Put down examples that will be used later for tests.

Decide what belongs to the functionality and what - to the changeable configuration. Extract functionalities and data entities from scenarios.

From scenarios make decision what your app will be. Is it service, activity, widget , even a content provider or a complex system, including some different components. Test your decision against scenarios.

In the case of the complex system, distribute functionalities and data entities among application components. Make a list of components and what they are (activities or smth else).

Make the list of UI components with description what they do (not HOW yet) These will be widgets and activities or fragments or layouts later.

Make draft layouts for UI components. Make simple passes from one to another. Look at the UI. Return to scenarios and play all of them them with your draft UI. All UI components and classes put into one hierarchy of packages or package.

Make a list of data entities. Decide what will be in what. Plan them as collections or tables in DB or different DBs. Make them as classes, put them into another hierarchy of packages or another package. Here also put DB helpers - classes that talk with DB by SQL.

Make a testing classes(JUNITs or better TestNG) for filling UI and data entities with test data and launching them.

Adapters needn't be public, for they are used in their parent GroupView only. So, no files for adapters usually.

Do not put all globals into special static classes - it is a bad practice. You are mixing so the code and the configuration. Use this very interesting solution. For now, it is the best I know for Android.

Configuration data should be put into resources. If some of them are complex, use XML sources and parser(s). Make readers of resource data into global variables. Not all of them will be static! They could belong to the main Activity instance, for example.

Do not use unconfigurable constants in the code! May be, your name only :-). Every constant becomes non-constant sometimes.

On the other side, if some of you code is not normal java, but scripts - a mix of data and language, then you can and must mix data and code.

Always do so:write something - connect something to a bulk - add test(s) for this new thing - test this new one - test the bulk - repeat. Small steps only!

share|improve this answer
add comment

Yes it make easy to develop if you categories thing on base their work.You are in right direction.In android,if develop an application then i will use following structure, something will similar like you

1)Main package With all Screen Activity

2)Adapter classes if i am using ListView, GridView that need BaseAdapter etc

3)Parsing helper if used

4)Database

5)Entity Classes and Classes to store static variable

6)Service and Receiver

share|improve this answer
add comment

In an Android Project manage code like this:

  1. In Activity first create an initializeViews() method and put all objects in this method; for example:

    private void initializeViews(){
     edittext= (EditText) findViewById(R.id.Example);
     // etc.....
    }
    

    initializeViews() is called in the onCreate() method.

  2. If some method and menu is implemented in all acivities then create one Baseactivity and have your other activities extend this class:

    public class BaseActivity extends Activity {
      // Implent Menu
    
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
    
      }
    
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
      }
    

    after that for example I have one activity name like A

    public class A extends BaseActivity {
      // now you access menu in this activity 
    }
    
  3. Also use a different package

    • for Activity:- MainPackage.Activity (All activitirs)
    • for Adapter :- MainPackage.Adapter (Adapter classes)
    • for Model :- MainPackage.Model (All models)
    • for Constant:- MainPackage.Constant (All constants)
  4. put all strings into String.xml

share|improve this answer
    
Programmers is about conceptual questions and answers are expected to explain things. Throwing code dumps instead of explanation is like copying code from IDE to whiteboard: it may look familiar and even sometimes be understandable, but it feels weird... just weird. Whiteboard doesn't have compiler –  gnat Feb 22 at 13:13
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.