Android - Switch Tabs from Within an Activity Within a Tab

Android - Switch Tabs from within an Activity within a tab

After a long time of battling with this problem I've been able to find a solution to switching tabs when using activity based tabs.

In the parent activity class where the tabhost is created I implemented a method like the one below:

public void switchTab(int tab){
tabHost.setCurrentTab(tab);
}

Inside of the tab that I would like to be able to switch internally to another tab I created the method below:

public void switchTabInActivity(int indexTabToSwitchTo){
MintTrack parentActivity;
parentActivity = (MintTrack) this.getParent();
parentActivity.switchTab(indexTabToSwitchTo);
}

If you would like a good example of this code, you can take a look at my MintTrack project here and here.

As a side note, please be very careful when deciding whether to use view or activity based TabHost.

Activity based tabs are great because they can be separated into there own XML file. Activities can also be organized into there own Java file instead of being cluttered into one. That being said some of the things you would think would be easy become complicated with activity based tabs. Its hard to pass information between tabs without creating overhead. Activity based tabs also use more memory/CPU time as they have the overhead of the Activity around each of them. Please consider this and the many more trade offs before diving into using an Activity based TabHost. I know now that I would personally go with a view based TabHost if I ever used them again.

How can I switch between tabs in a TabLayout programatically from within a fragment

I actually solved it now and it was quite easy.
To switch tabs from within the fragment an access to the tabLayout is required and using tabs = getActivity().findViewById(R.id.tabLayout); worked just fine for that and then just call tabs.getTabAt(1).select(); like snachmsm suggested in the previous answer.

Android - switch between tabs within activity, get index of tab

You will have to give the index with it. If you want to go to tB and it is on the second position.. You will use indexOfTabToSwitch(1);

How to switch between different activities like tabs in android?

Fragments will be more suitable for this scenario, the benefit are

  1. They are light weight and faster

  2. Managed automatically by the FragmentManager.

  3. Data Sharing between Fragments is smoother and simpler than it is for Activities

  4. They don't complicate the Architecture of the Application

You can have as many Fragments as you want in you Activity. Following two links can be useful for you.

A similar Thread

A Good Tutorial

Another good tutorial

If even after that you decide to use Activities, You need to think

Start a specific tab of a tab layout (with switch cases) from different activity

First get int extra from intent you're sending. Then if it's not null you invoke getItem(position) with default 0 and if it's not you pass the position and display the desired fragment.

 public class CategoryActivity extends AppCompatActivity {

public SectionsPagerAdapter mSectionsPagerAdapter;
public ViewPager mViewPager;
public int extrasPosition;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_category);
//get extra from intent;
Intent intent = getIntent();
if(intent == null) {
extrasPosition = 0;
} else {
extrasPosition = intent.getIntExtra("position", 0);
}

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

//display the desired fragment
mViewPager.setCurrentItem(extrasPosition);
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
// returning current tabs using switch case
switch (position){
case 0:
viewClothing tab0 = new viewClothing();
return tab0;
case 1:
viewElectronics tab1 = new viewElectronics();
return tab1;
case 2:
viewFurniture tab2 = new viewFurniture();
return tab2;
case 3:
viewGrocery tab3 = new viewGrocery();
return tab3;
case 4:
viewHardware tab4 = new viewHardware();
return tab4;
case 5:
viewStationary tab5 = new viewStationary();
return tab5;
case 6:
viewOthers tab6 = new viewOthers();
return tab6;
default:
return null;
}
}
@Override
public int getCount() {
// Show 7 total pages.
return 7;
}
}
}

Switching fragments within tab

So what happened was that, in TabListener, in the onTabUnselected method, the Fragment was not detached, causing it to still be show while a new Fragment was shown.

The cause to this was, that the Fragment that was detached was the first fragment, and not my second fragment. I've made some changes.

In the Activity:

SingleStationFragment singleStationFragment = new SingleStationFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(android.R.id.content, singleStationFragment, "STATIONS");
transaction.addToBackStack(null);
transaction.commit();

Here I've added the "STATIONS" tag in the replace() method, which is the same tag as the first fragment.

The TabListener is now as follows:

public class TabListener<T extends SherlockFragment> implements com.actionbarsherlock.app.ActionBar.TabListener {
private final SherlockFragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;

private SherlockFragment mFragment;

public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}

public void onTabSelected(Tab tab, FragmentTransaction ft) {
SherlockFragment preInitializedFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
if (preInitializedFragment == null) {
mFragment = (SherlockFragment) SherlockFragment.instantiate(mActivity, mClass.getName());
ft.add(R.id.treinverkeer_fragmentcontent, mFragment, mTag);
} else {
ft.attach(preInitializedFragment);
}
}

public void onTabUnselected(Tab tab, FragmentTransaction ft) {
SherlockFragment preInitializedFragment = (SherlockFragment) mActivity.getSupportFragmentManager().findFragmentByTag(mTag);

if (preInitializedFragment != null) {
ft.detach(preInitializedFragment);
} else if (mFragment != null) {
ft.detach(mFragment);
}
}

public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}

In the onTabUnselected method I now first retrieve the correct Fragment, then detach it.

Hope this helps someone!

How to switch tabs programmatically in Android from fragment?

Finally i can switch between the tabs programatically from Fragments using the following line of code

  TabHost host = (TabHost) getActivity().findViewById(android.R.id.tabhost);
host.setCurrentTab(2);

Hope it will help some one.



Related Topics



Leave a reply



Submit