How to make the back button switch between tabs
I have an application first showing a login screen which on a succesful 开发者_如何学Clogin changes to a tab view with 3 tabs. Each tab is containing a different activity. When I switch between the tabs I would like the back button to return to the previous tab instead of the login screen.
How should the be done? I'm considering implementing onBackPressed on the tab view and remembering the tab-stack manually but I don't feel that is the right approach.
Let say you have tabs created in class called Main.java. Create there method like this:
public void switchTab(int tab) {
tabHost.setCurrentTab(tab);
}
which will change current tab. Next in activity where you want to change navigation insert the following code:
@Override
public void onBackPressed() {
Main m = (Main)getParent();
m.switchTab(0);
}
This should work.
精彩评论