3

I have an activity with five tabs. Everything looks okay when I go from tab 1 to tab 2 or tab 3. How can I go back programatically from tab 2 to tab 1?

    Intent myIntent = new Intent(this, Tab1.class);             
    myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    startActivity(myIntent);

This is not working properly because it starts activity 1 without any tab.

When going from tab 1 to tab 2 I can both see tab 1 and tab 2 (current tab activated). But when going from tab 2 to tab 1, both tab 1 and tab 2 disappear from the activity. What could cause this?

SALMAN
  • 3,909
  • 1
  • 24
  • 21
just ME
  • 1,781
  • 6
  • 30
  • 53
  • check http://stackoverflow.com/questions/3871681/android-how-to-change-activity-within-a-tab. – jeet Jul 24 '12 at 12:25
  • see this [link](http://stackoverflow.com/questions/4307235/tabhost-obtaining-previous-tab-after-a-tab-change) possibly same question – Archana Jul 24 '12 at 12:32

4 Answers4

2

This will surely help you.

TabHost tabHost =  (TabHost) getParent().findViewById(android.R.id.tabhost);
tabHost.setCurrentTab(1);  

OR you can refer to this link

How to programmatically switch tabs using buttonclick in Android

Thanks :)

Community
  • 1
  • 1
SALMAN
  • 3,909
  • 1
  • 24
  • 21
  • i am going from activity2 to activity1.what did you post? – just ME Jul 24 '12 at 12:22
  • Your activities must be a TabActivity ..if you want to programmatically navigate between tab2Activity to tab1Activity use my code . Thanks :) – SALMAN Jul 24 '12 at 12:24
  • @Angela If my answer helps you than please dont forget to accept my answer and up vote me it will be much appreciated. Thanks :) – SALMAN Jul 24 '12 at 12:36
1

just use finish() method

public void onClick(View v) 
{                             
    finish();
    startActivity(new Intent(Activity2.this, Activity1.class));            
}     
NagarjunaReddy
  • 8,500
  • 10
  • 60
  • 94
0

I don't know about the Intent.FLAG_ACTIVITY_CLEAR_TOP, never needed that, but the mentioned effect of loosing your tabs is produced by calling startActivity() from your TabHost, not one of your tabs. If that's the case, move the call there and your tabs should stay.

Cdr. Powell
  • 722
  • 6
  • 19
0

I have a similar situation but seems none of the answers help. so, I post my solution here:

// tab selection history, each tab has a tag which is a string
private List<String> tabIdHistory = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstanceState);
    // this layout contains TabHost and TabWidget
    setContentView(R.layout.activity_main); 

    TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
    tabHost.setup();
    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            tabIdHistory.remove(tabId); // ensure uniqueness
            tabIdHistory.add(tabId);
        }
    });
    // continue your tab initialisation, such as
    // tabHost.addTab(tabHost.newTabSpec(TAG)
    //         .setContent(...).setIndicator(...));
}

@Override
public void onBackPressed() {
    if (tabIdHistory.size() > 1) {
        // pop the current last item, we want the second last
        tabIdHistory.remove(tabIdHistory.size() - 1);
        tabHost.setCurrentTabByTag(tabIdHistory.get(tabIdHistory.size() - 1));
    } else {
        super.onBackPressed();
    }
}

If use select tab#1, tab#3, tab#2, tab#1, then the back stack is "3, 2, 1" and app will exit to main screen if user press back button three times. If you want to keep full history, comment out this line:

tabIdHistory.remove(tabId);

John Pang
  • 1,952
  • 19
  • 23