How to communicate between Android tabs

10,626

You definitely want to reconsider using Activities as the content of your tabs. The more standard approach is to use one Activity that uses Tabs to only show part of the layout when a particular tab is selected.

The Android documentation has an excellent worked example, check out Hello, TabWidget.

Alternative

If for some reason you do need to use Activities, you can pass information between them by either adding values to the extras bundle within the Intent your using to open each Activity, or by extending the Application class.

By extending the Application class (and implementing it as a Singleton) you get an object that will exist whenever any of your application components exist, providing a centralized place to store and transfer complex object data between application components.

Share:
10,626
Vidar Vestnes
Author by

Vidar Vestnes

I'm focused on mobile platform applications. Android apps: WriteDiary Zedge Website: HelseSmart (Norwegian) ORGi (Norwegian) More details about me at LinkedIn

Updated on June 04, 2022

Comments

  • Vidar Vestnes
    Vidar Vestnes almost 2 years

    Im trying to setup some tabs for my android application, but i got stuck.

    I cant find a way to communicate between the tabs..

    I got 2 tabs.


    |Search|Result|

    The search tab is simply showing a TextEdit and a "Search" button. Hitting the search button should make my app change to the result tab and display the result.

    i have added the tabs as activities using new intents as

    TabHost host=getTabHost();  
    
    host.addTab(host.newTabSpec("one")  
       .setIndicator("Search")  
       .setContent(new Intent(this, SearchTabActivity.class)));  
    host.addTab(host.newTabSpec("Result")  
       .setIndicator("Android")  
       .setContent(new Intent(this, ResultTabActivity.class)));  
    

    Now i cant find a way for SearchTabActivity to display ResultTabActivity and alter its view...

    Im looking forward for any tip.

  • Okku
    Okku almost 13 years
    Is this still the case? That you should definitely want to reconsider using Activities as the content of your tabs.? The app I am working with has four tabs, 2 listviews, 1 map view and 1 info scrollview...
  • Carlo Chum
    Carlo Chum almost 10 years
    Yes, you can use listfragments and can place scrollviews inside the other fragment. The mapview may be a little trickier, I have not used them before but this answer should help.