0

I am new to using TabHost and currently trying to implement a validation check when moving between tabs. My problem comes with the use of

onTabChanged(String tabId)

as i need to know what the selected tab is so as, if validation fails, i can redirect to the previous tabs. How can i get the id's so as to know the current tab and redirect to previous tab?

My tabhost script is:

import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;


public class Invoice3 extends FragmentActivity implements OnTabChangeListener {
private FragmentTabHost mTabHost;
String check = null;
ShowAlert alert = new ShowAlert();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main2);
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);

    mTabHost.addTab(
            mTabHost.newTabSpec("tab1").setIndicator("Client", null),
            clientFragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab2").setIndicator("Products", null),
            productsFragmentTab.class, null);
    mTabHost.addTab(
            mTabHost.newTabSpec("tab3").setIndicator("Confirm", null),
            confirmFragmentTab.class, null);

}

public void onTabChanged(String tabId) {
    //Log.d(TAG, "onTabChanged(): tabId=" + tabId);
    check = GlobalApp.data().value;

    if (<check if 2nd tab has been selected>) {

        if(check == "False")
        {
          <redirect to 1st tab>
        }
    }

    }
}


}
user3682205
  • 241
  • 4
  • 16

1 Answers1

0

You can save in a variable the id of the current tab, and go back to it. Something like this should work.

String prevTab = null;
public void onTabChanged(String tabId) {
    //Log.d(TAG, "onTabChanged(): tabId=" + tabId);
    check = GlobalApp.data().value;
    if (<check if 2nd tab has been selected>) {
        if(check == "False")
        {
            if (prevTab != null) {
                // redirect to prevTab
            }
        } else {
            prevTab = tabId;
        }
    }
}
Sebastian
  • 1,076
  • 9
  • 24