0

I want to start different activities depending on the click of the tab bar item but I get a blank page as started activity in the "windows class". What I intended to do is start different activities on click of tab bar item.

Tab Bar Activity Class

import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TabHost;


public class TabBar extends FragmentActivity {

    ViewPager Tab;
    TabPagerAdapter TabAdapter;
    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabbardefault);


        try {

            TabAdapter = new TabPagerAdapter(getSupportFragmentManager());

            Tab = (ViewPager) findViewById(R.id.pager);
            Tab.setOnPageChangeListener(
                    new ViewPager.SimpleOnPageChangeListener() {
                        @Override
                        public void onPageSelected(int position) {

                            actionBar = getActionBar();
                            actionBar.setSelectedNavigationItem(position);
                        }
                    });
            Tab.setAdapter(TabAdapter);

            actionBar = getActionBar();
            //Enable Tabs on Action Bar
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        }
        catch (Exception e)
        {

        }
        ActionBar.TabListener tabListener = new ActionBar.TabListener(){

            @Override
            public void onTabReselected(android.app.ActionBar.Tab tab,
                                        FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {

                Tab.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(android.app.ActionBar.Tab tab,
                                        FragmentTransaction ft) {
                // TODO Auto-generated method stub

            }};


        //Add New Tab
        actionBar.addTab(actionBar.newTab().setText("Home").setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("Celeb").setTabListener(tabListener));
        actionBar.addTab(actionBar.newTab().setText("Videos").setTabListener(tabListener));


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_tab_bar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

TabPagerAdapter

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

public class TabPagerAdapter extends FragmentStatePagerAdapter {
    public TabPagerAdapter(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int i) {
        switch (i) {
        case 0:
            //Fragement for Android Tab

            return new Android();
        case 1:
           //Fragment for Ios Tab
            return new Ios();
        case 2:
            //Fragment for Windows Tab
            return new Windows();
        }
        return null;

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3; //No of Tabs
    }


    }

Windows

    public class Windows extends Fragment {
     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View windows = inflater.inflate(R.layout.feed_main, container, false);
            //((TextView)windows.findViewById(R.id.textView)).setText("Windows");
            return windows;
}}
Prabhu Konchada
  • 548
  • 8
  • 26

1 Answers1

0

To answer you, you are not actually starting an Activity you a switching through Fragments, Fragment is just a Custom View, and you inflate an xml to represent the Custom View which is the Fragment, if you want to start an Activity after your Tab is selected then you call startActivity(Intent); startActivity(new Intent(classname.this,the_class_i_want_to_start.class));

and your Windows class extends Fragment it should be Activity if what you want is the functionality of Fragment then you aint got a problem

hope its lucid

Elltz
  • 10,730
  • 4
  • 31
  • 59