Open activity "B" from tabspec TA1 of tabhost and on backpressed of activity "B" tabhost should be set to "TA1" tabspec

StackOverflow https://stackoverflow.com/questions/23534327

  •  17-07-2023
  •  | 
  •  

Question

I am having four tabspec in tabhost TA1,TA2,TA3,TA4 in which i set current tab as TA2, I set four different activities for four tabspec A1, A2, A3 and A4 respectively, Activity A1 is set to TA1 and so on,then Activity A1 Open new activity B1, when i pressed back button from activity B1 the tab should be set to TA1 not to default TA2, How can i achieve above task in tab host, I tried to store current index of tab into shared preferences and onResume i read int value of current tab from shared preferences, but this way i was not able to achieve above task, Please let me know if anyone knows the best solution.

Was it helpful?

Solution 2

Hi Friends i resolved my issue by passing value through intent to main tab activity, here below is sample code,Set this code lines to child activity of tab that means if i relate to above question child activity is "B1",

Intent homeIntent = new Intent(SelectFetchee.this, Home.class);
            homeIntent.putExtra("tabvalue", "2");
            startActivity(homeIntent);
Then in main activity that have tabhost in which i collect the string

String crntTab = getIntent().getStringExtra("tabvalue");

        if (crntTab == null) {
            tabHost.setCurrentTab(2);
        } else if (crntTab.toString().equals("4")) {

            tabHost.setCurrentTab(4);
        } else if (crntTab.toString().equals("2")) {
            tabHost.setCurrentTab(2);
        } else if (crntTab.toString().equals("3")) {
            tabHost.setCurrentTab(3);
        } else if (crntTab.toString().equals("0")) {
            tabHost.setCurrentTab(0);
        } else if (crntTab.toString().equals("1")) {
            tabHost.setCurrentTab(1);
        }

OTHER TIPS

You can override this function to check click on back button of device.

@Override
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) 
{
    // implement your Tab move code from one tab to another. 
}
return super.onKeyDown(keyCode, event);
}

Reena you should use handler in your tab Activity and send a message from activity B on back press button, when you will fire back press a message will be send to Tab Activity and on Handle message of handler you can set currentTab to Activity TA1.

use this handler in TAbActivity.

    static Handler mHandler;

and in onCreate

tabHost.setCurrentTab(1); //For TA2
    setTabColor(tabHost);

    mHandler = new Handler()
    {
        public void handleMessage(android.os.Message msg) 
        {
            super.handleMessage(msg);
            if (msg.what == 0)
            {
                tabHost.setCurrentTab(0);//for TA1
            }

        }
    };

And OnBackPress of Act B use this

            Message msgtab=HomeTab.mHandler.obtainMessage();
            msgtab.what=0;
            HomeTab.mHandler.sendMessage(msgtab);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top