jay
jay

Reputation: 434

Android Multi-Screen Application

How do you handle multiple screens in an android application? I have developed with the tab bar at the bottom without problem, however what I am wanting to do is replace all the content on the screen with the content from a new .xml layout file I have created in the project. Additionally, how would I tie the back end code to the new layout file? I'm sure this question probably exists already and is googleable (may have made up a new word). However, I don't know exactly what it is that I am looking for. Thanks in advance for your help.

Upvotes: 6

Views: 20434

Answers (4)

Tommy
Tommy

Reputation: 1267

What you need to do is, create a new Activity and add it to the AndroidManifest.xml:

<activity android:name="ActivityClassName" android:label="Label for the Activity"></activity>

and can be called in a method:

public void startActivity() {
    Intent someName = new Intent(CurrentClass.this, ActivityClassName.class);
    startActivity(someName);
}

Upvotes: 6

Joseph Earl
Joseph Earl

Reputation: 23432

It really depends on how you want your application to flow.

Let's consider the scenario where a user does the following:

  1. Starts your first activity
  2. Presses the 2nd tab
  3. Presses the 3rd tab
  4. Presses the back button

If you use a separate activity for each screen, then the following would happen

  1. Activity 1 is started
  2. Activity 2 is started
  3. Activity 3 is started
  4. Activity 3 is closed, user returns to Activity 2

(in this case pressing the back button again would you take you back to Activity 1, and pressing it again would exit your application)

If you used one activity for all the tabs, then the following would occur

  1. Activity 1 is started
  2. Activity 1 sets tab content to tab 2 content
  3. Activity 1 sets tab content to tab 3 content
  4. Activity 1 is closed, user returns to home screen

If you are using a screen with tabs, then the second method (a single Activity with a TabHost or similar) is the preferred method, otherwise the user will end up making a large activity-stack just switching between tabs (meaning if they switch between tabs a lot they'll have to press the back button a lot of times to exit).

If you want to go for the single activity approach, then do some research on TabHost and TabContentFactory. In the createTabContent method of your factory you can inflate a View/layout from XML to set as the tab content using View.inflate. Look those up and come back ask another question if you get stuck ;)

Upvotes: 5

Phil Lello
Phil Lello

Reputation: 8639

Android applications generally use a separate Activity for each screen, and switch between them using Activity.startActivity and Activity.startActivityForResult. You can pass arbitrary data to an Activity via Intent.putExtra.

Hope this helps,

Phil Lello

Upvotes: 5

Farhan
Farhan

Reputation: 13390

i think you may want to play with more than one activity.... you can have multiple activities and one xml for each of them... in this way you can have different screens... check these links. Multiple Activities, Creating an Activity.... hope this helps...

Upvotes: 4

Related Questions