Rob
Rob

Reputation: 15992

Need some explanations about Android fragments and ActionBar tabs

I am trying to understand Android fragments and navigation, but there is something I just don't know how to do. I have created an app, with a MainActivity containing a viewPager :

public class MainActivity extends FragmentActivity implements TabListener
{
    private ViewPager viewPager;
    private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    private String[] tabNames = {"Tab 1", "Tab 2", "Tab 3"};

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

        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        for(int i = 0 ; i < tabNames.length ; i++) 
            actionBar.addTab(actionBar.newTab().setText(tabNames[i]).setTabListener(this));
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {}

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft)
    {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {}
}

Here is TabsPagerAdapter :

public class TabsPagerAdapter extends FragmentPagerAdapter
{
    public TabsPagerAdapter(FragmentManager fm)
    {
        super(fm);
    }

    @Override
    public Fragment getItem(int index)
    {
        if(index == 0) return new FirstFragment();
        else if(index == 1) return new SecondFragment();
        else return new ThirdFragment();
    }

    @Override
    public int getCount()
    {
        return 3;
    }
}

And my FirstFragment is a list so it extends ListFragment. Here is what it looks like :

enter image description here

Now I want to go to another view if I click an item. Before I used to do it like this in apps without action bar and tabs :

Intent i = new Intent(this.getActivity().getApplicationContext(), MyNewActivity.class);
startActivity(i);

But now when I do this it doesn't display the action bar on top of the screen anymore, and I also want to keep the navigation state on this tab, if I go to another tab and then come back. What should I do?

Thanks for your help.

Upvotes: 0

Views: 115

Answers (1)

SavageKing
SavageKing

Reputation: 544

It is better to let each individual fragment manage its own menu items (actionbar) so you have to call setHasMenuOptions(true) in each fragment that you want to have menu options in. Get a reference to the actionbar in onActivityCreated() and configure your actionbar how you want it there. You will also have to override the oncreateoptionsmenu and onOptionsItemSelected in the fragment to handle menu item clicks.

Also using the view pager and tabs you want to make each tab a fragment. I don't know about making each tab an activity, and I don't even think that is possible, and if you are doing that then that is your problem. I don't see that from your code, and that is good.

Each tab needs to be a Fragment, so convert all of your activities into fragments and then use the supportFragmentManager to dynamically add and replace fragments to your framelayout resource, or override getItem and return the correct fragment as needed.

Upvotes: 1

Related Questions