Search code examples
androidandroid-activityorganization

How to structure my android application


I'm very new to Android development, and want to make sure that I'm structuring my application correctly. First, let me explain what is needed.

The application starts off prompting the user for an access code, depending on their response there are two resulting menu's which can appear. One menu has 5 buttons, while the other adds two extra buttons making seven. Each one of those buttons brings me to a different view where more information will be displayed.

I originally starting writing it with one activity and a different XML file for each view. However, the more I have been researching online it seems that I should have a different Activity for each individual view. But now I'm relatively confused how I can prompt the user for input before initializing any of the Activities.

If anyone has any input I'd really appreciate it.

Thanks


Solution

  • You will need to initialize an activity before getting user input. And I think it is common that if you go to a new view that it uses a different class and xml layout. So for each of the new views you could make a new class that extends an activity and then has an xml file related to that view.

    So have these 2 files for each new view you show.

    Java file:

    public class Activity1 extends Activity {
       @Override
       public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.layout1);
       }
    }
    

    XML file:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/layout1"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >
    
      //add any views
    
    </LinearLayout>