1
votes

I am using tab host(tabs) in application, it have four tabs on home screen, I have set new layouts on every tabs, I want to that once user clicking tabs like: tab1>tab2>tab3>tab4 and press back on tab4 he/she should go back to tab3 and on tab3 pressing back go to tab2 and so on. I didn't get any idea, how to implement? Could any one help me on this, any help will be appreciated. Thanking you all..!!

5

5 Answers

2
votes

Create the below method in your class which extends TabActivity,

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

And now in your activity's back press method, do this,

public void onBackPressed() {
    TabBar parentTab = (TabBar) this.getParent();
        parentTab.switchTabBar(1);//Instead of 1 provide the tab position which you want to navigate to. 

}
0
votes

you can override onBackPressed() and inside it do what you want.

0
votes

I dont know why you want to add back button.In tabs there's no need to add back button.See the below link

http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

0
votes

You need to create some sort of a activities-backstack (if you use nested ActivityGroup), override onBackPressed() method in your TabActivity and manage this backstack in onBackPressed() method.

0
votes

You have 2 options:- with Tab or without Tab.

If it is not necessary that you have to use tab in your code then instead of tab you can use buttons and you can easily get your goal. But if you want to use tab then you have to Override onBackPressed() in every 4 Activity.

I hope you got any idea according to my answer. I hope it helps you.