简体   繁体   中英

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

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>

Try:

-push activity1 with layout1
-pop inputDialog
-when inputDialog is closed by clicking ok...
-push Activity2 with layout2, proceed with you input from activity1 using extras

...and so on ;)

I have been trying to break up my programs into an activity and a corresponding xml layout for each view. If you have one activity and all those layouts, you have the potential to have a monster block of code in that one activity. I find that breaking it up makes it easier to read and debug.

As for prompting the user before initializing activities, i'm not entirely clear on what you mean. You need to load an activity before anything happens, in your situation it could easily be a simple password acception activity. If you're talking about passing information between activities, you can package data in an intent, and use that to start a new activity. Then in that new activity pull the information out of the intent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM