Android: TabActivity deprecated, use Fragments?
Asked Answered
L

5

9

It appears that TabActivity is now deprecated, we need to use Fragments.

I tried using a standard Activity but couldn't call getTabHost.

So it appears i have to use Fragments. But i am a little confused how this would work.

I understand that fragments are not activities so they are not in the manifest file?

SO i presume i can't do startActivity on a fragment?

Does anyone know of a good example explaining the tabHost and Fragments, all the examples and tutorials i have found are only using the tabactivity.

Thanks in advance

Leverrier answered 12/9, 2012 at 6:32 Comment(0)
T
1

Maybe you could use TabLayout instead.

Tabs are now best implemented by leveraging the ViewPager with a custom "tab indicator" on top. Google's new TabLayout included in the support design library release for Android "M".

Visit the full tutorial of using TabLayout in Google Play Style Tabs using TabLayoutEdit PagePage History

Therontheropod answered 22/6, 2016 at 6:57 Comment(0)
M
0

Today Android got a nice tutorials for those. Start at Creating Swipe Views with Tabs

Here is a small snapshot of how to create tabs

@Override
public void onCreate(Bundle savedInstanceState) {
    final ActionBar actionBar = getActionBar();
    ...

    // Specify that tabs should be displayed in the action bar.
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create a tab listener that is called when the user changes tabs.
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            // show the given tab
        }

        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
            // hide the given tab
        }

        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
            // probably ignore this event
        }
    };

    // Add 3 tabs, specifying the tab's text and TabListener
    for (int i = 0; i < 3; i++) {
        actionBar.addTab(
                actionBar.newTab()
                        .setText("Tab " + (i + 1))
                        .setTabListener(tabListener));
    }
}
Monomerous answered 17/1, 2016 at 11:32 Comment(0)
G
0

First - you can startActivity from your fragment by something like context.startActivity(....);

Second to understand the tabs using fragment just create one sample project on your Android Studio and when it asks you to choose the existing template then choose the one which is having tabs. This way you will see the standard code.

Gorgias answered 17/1, 2016 at 13:30 Comment(0)
A
0

Lets face it. Fragments are the future. And we are being guided towards using them.

I found the following tutorials to be quite informative and hope they would answer all your 'Tabs with Fragments questions':

Almaalmaata answered 22/6, 2016 at 6:44 Comment(0)
I
0

I think your problem with using selected realization. Android SDK contains a lot of variants of View, such as ActivityList, TabActivity, ActionBarActivity, etc. And they all deprecated or will deprecated. You should ask why?

  • Look at the different example with Tabs and List in new Material. Application contains main header, like ToolbarLayout, and in this contains different state of Toolbar, you may add TabLayout, and other cool things.

So answer on your question - It's better to use customize and powerful view elements, then several implementations of main action view (Activity). This reason still more visible, when you look to action specification of elements. Activity don't need to take place in view initialization.

Ingalls answered 22/6, 2016 at 7:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.