Android Tabs: Activities or Views?
I'm trying to write a little Android application to st开发者_JAVA百科ore recipes as practice. To add a new recipe, I'm trying to use tabs. There are three tabs,
- Ingredients
- Steps
- Media
I'd like "Ingredients" to have a ListView, "Steps" to have a ListView, and "Media" to have a gallery of images. I'm going to add new ingredients, steps, and media through some option in the options menu. When finished, I'd like to write everything to an SQLite database.
I'm wondering if I should use different activities or just different views for each tab?
If I use different activities, would it be hard to pass information between the activities?
If I use different views, do the views all have to be part of the same layout file? For example, the tutorial on TabWidgets does the following:
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/textview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a tab" />
<TextView
android:id="@+id/textview2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is another tab" />
<TextView
android:id="@+id/textview3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="this is a third tab" />
</FrameLayout>
So all the TextViews are within the same FrameLayout, with each TextView being used for only one tab. I think this would be a little confusing if the views were more complex, e.g. something nested within something else, which is again nested within something else. Using different activities for each tab, I can specify a different layout file for each tab.
I'm wondering if I should use different activities or just different views for each tab?
Using Activities as Tab content has been deprecated. You can still do it but if you're just starting out with this project then using Views instead would be a better approach.
If I use different activities, would it be hard to pass information between the activities?
Yes and no, but it depends on your level of expertise - passing information between Activities generally involves using Intents (although there are other ways). Although this is the same whether they're stand-alone or part of a tabbed environment doesn't make any difference but it does take some thinking about.
If I use different views, do the views all have to be part of the same layout file?
No but you might want to experiment with the approach in that tutorial when using a TabActivity to get started.
It's possible to have different layouts (for any type of Activity) and inflate them yourself as necessary but that's a more complex topic.
I'm not sure which is better but based on your question about whether it is hard to pass information between different activities is fairly simple to do.
If you have two activities, activityA and activityB and you want to send information from A to B then you can use the following
Intent intent = new Intent(activityAClassName.this, activityBClassName.class);
intent.putExtra("keyName", "value");
startActivity(intent);
Then to get the values from activityA in activityB and this code into activityB
Bundle bundle = this.getIntent().getExtras();
string value = bundle.getString("keyName");
hope this helps
精彩评论