简体   繁体   中英

Passing “events” from child tab content Activity to parent TabActivity

I setup a basic TabActivity very similar to Google's tab tutorial. However, above the TabWidget I have a TextView that I'd like to update and change the text after certain events get fired in each child tab/child activity (not necessarily when switching between tabs). I realize I may not be able to bind events between activities so any ideas on how to achieve this? Setup a service that both the TabActivity and children Activities communicate through? Is it even possible to get the TextView here in the TabHost to even redraw while a child activity is active?

My TabActivity inflates the following view:

<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 android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <TextView android:id="@+id/textToUpdate" android:text="Some text to update" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>

    <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" />
</LinearLayout>

After doing some very ugly workarounds, finally figured out a very simple solution. From within the child activity, I added:

protected void updateParentText(){
    MyTabActivity parent = (MyTabActivity) this.getParent();
    TextView tv = (TextView) parent.findViewById(R.id.textToUpdate);
    tv.setText("New Text here");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM