Reputation: 4259
I haven't been working with android 3.0+, nor the action bar, so I have a question about the tabs that can be implemented in the action bar. Is it only restricted to changing fragments? Can the tabs be: the first an activity, the second fragment? From the examples I checked out, there is one main activity that has an action bar and the content of each tab is a separate fragment.. Sorry if my question is something obvious. Thank u in advance.
Upvotes: 0
Views: 605
Reputation: 541
There isn't anything that says you have to switch fragments, even though the base code is structured to make it easy to do.
For example, I just use tabs to change the visibility of elements in a once-loaded view layout hierarchy, and that works fine. This seems much more efficient if your view hierarchy is not that complex, as it eliminates fragment transactions. Presumably, changing the visibility of a view is pretty efficient.
Here's an incomplete code fragment to give you an idea of how to ignore the fragment transactions and do your own thing:
public class AudioManagerTabListener<T extends Fragment> implements
ActionBar.TabListener {
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
switch ((Integer) tab.getTag()) {
case TAG_SHARED:
mGridViewWrapper.setVisibility(View.VISIBLE);
break;
case TAG_PLAYING:
mNowPlayingWrapper.setVisibility(View.VISIBLE);
break;
case TAG_PLAYLIST:
break;
case TAG_ARTISTS:
break;
case TAG_ALBUMS:
break;
case TAG_SONGS:
break;
}
if (DBG.AUDIO) {
Log.d(TAG,
"SettingsTabListener- onTabSelected - Tag: "
+ tab.getText());
}
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
switch ((Integer) tab.getTag()) {
case TAG_SHARED:
mGridViewWrapper.setVisibility(View.GONE);
break;
case TAG_PLAYING:
mNowPlayingWrapper.setVisibility(View.GONE);
break;
case TAG_PLAYLIST:
break;
case TAG_ARTISTS:
break;
case TAG_ALBUMS:
break;
case TAG_SONGS:
break;
}
if (DBG.AUDIO) {
Log.d(TAG,
"SettingsTabListener- onTabUnSelected - Tag: "
+ tab.getText());
}
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
if (DBG.AUDIO) {
Log.d(TAG, "SettingsTabListener- onTabReselected- Position: "
+ tab.getPosition());
}
}
}
Upvotes: 1
Reputation: 2843
I dont see the advantage of you using an Activity as the first over using Fragments for them all? Fragments are very powerful and can provide just as much as an Activity.
Having said that, you should be able to get a callback when a Tab is selected, and so in that callback all you have to do is load up an Activity. Make sure you pass in the currently selected tab when you load up the new Activity so that the user can see which one the had selected.
The reasoning behind using a Fragment instead is that you will not have to load up a new Activity, and you can replace the current Fragment with your new Content.
Upvotes: 2