Monday, April 4, 2011

Returning to a Specific Tab from Another Activity

In the Connexions for Android app, there is a TabActivity that has 3 tabs as shown in the screenshot. If the user preforms an action in any of the tabs, the next Activity moves outside the TabActivity.

On all of the Activities outside the TabActivity, there is a menu option to return to one of the Tabs (Search, Lenses, Favorites). One of the problems I had to solve was how to get the TabActivity to display the tab selected by the user outside of the TabActivity. All of the menu code is in one class, MenuHandler. In MenuHandler, if a tab is selected from the menu, an extra is added to the intent for the TabActivity. The extra tells the TabActivity which Tab to display.
'''
case R.id.go_to_favs:
  Intent intent = new Intent(context, TabWidget.class);
  intent.putExtra(TabWidget.TAB_ID, new Integer(2));
  context.startActivity(intent);
  return true;
'''

The extra is retrieved in the onCreate() of TabWidget (the TabActivity in the app). If there is a value for the extra, the current tab is set to it, Otherwise the current tab is set to the first tab.
...
Integer bigInt = (Integer)getIntent().getSerializableExtra(TAB_ID);
if(bigInt != null)
{
  tabHost.setCurrentTab(bigInt.intValue());
}
else
{
  tabHost.setCurre ntTab(0);
}


As always, you can browse the source or download a zip file of the source. Connexions for Android is available in the Android Market or from the Connexions website.

6 comments:

  1. Hi Ed,
    Excelent tutorial, but i didn´t find the TabWidget activity in your source code

    ReplyDelete
  2. Ruben: Sorry about that. You can download the code using

    svn co https://software.cnx.rice.edu/svn/labs/CNXAndroid/branches/ca-1.8

    I'll update the link in the post as well.

    ReplyDelete
  3. The download link now points to the correct zip file.

    ReplyDelete
  4. Ed, Thanks a lot for you help, i´ll check the file

    ReplyDelete
  5. That was i need to complete a project, thanks a lot bro.

    ReplyDelete