Senthil Mg
Senthil Mg

Reputation: 3313

Need Android tabhost back press navigation

Friend's, I implemented tabhost in my application,i have three tabs defined in my activity tab1,tab2,tab3...here i have a problem to navigate from tab3 to tab1 at onkey back pressed event ,how can i reach the tab1 from tab3 and tab2,i tried up using overided keyback pressed,but it some times it's not responding to navigate.without overriding onkey back pressed it simply exits from application.

Here my code

tabHost= getTabHost();

        tabHost.addTab(tabHost.newTabSpec("tab1").setContent(
                new Intent(this, DealCities.class)).setIndicator(prepareTabView("Deals",R.drawable.test)));

        tabHost.addTab(tabHost.newTabSpec("tab2").setContent(new Intent(this, DbServerActivity.class))
                        .setIndicator(prepareTabView("My QuadDeals",R.drawable.mydeal)));

        tabHost.addTab(tabHost.newTabSpec("tab3").setContent(new Intent(this, Account.class))
                .setIndicator(prepareTabView("My Account",R.drawable.img_myaccount)));

here the tabhost defined in one class and the tabhost object being static one,

here the code from my tab2 back pressed event has follows,

 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

            try {               
//              userdealList.setVisibility(View.GONE);              
//              logalertTable.setVisibility(View.GONE);
//              dealType.setVisibility(View.GONE);
//              mydealBack=1;
//              QuadMain.tabHost.setCurrentTab(0);  
                Intent i = new Intent();
                i.setClass(DbServerActivity.this, QuadMain.class);
                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);

            } catch (Exception e) {

            }
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

it works fine sometimes only and sometimes it's not working..

Thanks in advance.

Upvotes: 0

Views: 3261

Answers (2)

Squonk
Squonk

Reputation: 48871

Don't handle KeyEvent.KEYCODE_BACK in your tab activitys, do it in your main TabActivity.

As long as your tab activitys onKeyDown() methods don't handle it but they do call super.onKeyDown(keyCode, event); it will filter up to your TabActivity.

In your TabActivity have a member int currentTab = 0 then in the TabActivity do this...

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (currentTab == 0)
            currentTab = 2;
        else
            currentTab--;
        tabHost.setCurrentTab(currentTab);
        return true;
    }
    else
        return super.onKeyDown(keyCode, event); 
}

Upvotes: 2

Vivienne Fosh
Vivienne Fosh

Reputation: 1778

Check out this manual http://knightswhocode.com/wordpress/?p=46 - teaches how to use TabHost. Unfortunately by default onkeyBackPressed event you just go to the previous activity and as you have one main tabhost activity it just quits. I suggest you overriding the back key pressed and use the following method:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        YourParentActivity parentActivity;
        parentActivity = (YourParentActivity) this.getParent();
        parentActivity.switchTab(indexTabToSwitchTo);
        return true;
    }
    return super.onKeyDown(keyCode, event); 
}

Upvotes: 3

Related Questions