Search code examples
androidandroid-tabhosttabwidgetandroid-actionbar

How to get a view from an activity, which is located inside a tab, while the view is located outside the tab


I have a layout which inludes actionbar and tabs:

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout style="@style/BackgroundLayer">

        <!-- Action Bar with buttons -->
        <include layout="@layout/incl_actionbar"/>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" />
    </LinearLayout>

</TabHost>

And on the actionbar I have several buttons that I need to be able to access from activities within tabs. But findViewById returns null for any view located on the actionbar - I find this quite logical, because for any activity inside tab their contentRoot is FrameLayout (and actionbar is ourside of it), but nevertheless I need an access to the actionbar, because icons for all currently available actions are located there.

I also have thought of passing actionbar as extra to intents for all activities inside tabs, but View is not serializable nor Parcelable, so it can't be passed as extra.


Solution

  • Let TabActivity provide access to actionbar. Child activities should request actionbar using the following code, e.g.:

    View actionBar = ((CustomTabActivity) getParent()).getActionBar();