I had a slightly different problem, and I thought that I would add this for everyone who is faced with a similar situation. I have an activity-based tabbed application, and one kind of tabs launches another action that is not controlled by tabHost. I needed to press a button on this completion of the activity () (i.e., return to the main view of the tab) and at the same time switch to another tab.
I decided to process it using BroadcastReceiver. In the class that sets up TabHost, I added this class:
class ChangeTabReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "ChangeTabReceiver: received"); TabHost tabHost = getTabHost(); tabHost.setCurrentTab(1); } }
.. then defined vars:
ChangeTabReceiver changeTabReceiver; IntentFilter changeTabFilter;
.. then added to onCreate ():
changeTabReceiver = new ChangeTabReceiver(); changeTabFilter = new IntentFilter(myApplication.CHANGE_TAB); registerReceiver(changeTabReceiver, changeTabFilter);
Finally, in the new action, when you want to close this action and switch tabs, do the following:
Intent intent = new Intent(myApplication.CHANGE_TAB); this.sendBroadcast(intent); this.finish();
Of course, you can make a way to switch to different tabs by passing the tab index, but in my case this behavior happens in only one action, so I decided to keep it simple ...
bkurzius Jul 09 '11 at 10:21 2011-07-09 22:21
source share