简体   繁体   中英

How to call a function in TabActivity from a child activity?

I want to update the title of the tab according to the content of my child activity.

However, when I call:

TabActivity parent = (TabActivity) getParent();
TabHost parentHost = (TabHost) parent.getTabHost().findViewById(android.R.id.tabhost);

in the child, the program crashes. * (Thanks StinePike) Now I know I cannot get the instance of TabHost, but still, I can't call the methods defined in my own TabActivity. *

Anyone can help me discover the problem and solve it?

My TabActivity setup:

public class Tab extends TabActivity {  
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.tab);

        final TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);

        View newTab = (View) LayoutInflater.from(this).inflate(R.layout.tabmini, null);
        TextView text0 = (TextView) newTab.findViewById(R.id.tab_label);
        text0.setText("new tab");

        tabHost.setup(this.getLocalActivityManager());
        tabHost.setCurrentTab(1);

        tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(newTab).setContent(new Intent(this, BrowserActivity.class)));

    }

    public void addTab(String startPage){
        //...Add a new Tab  
    }   
    public void setTabTitle(String title){
        //...Set new title  
    }   

}

from child call

RootActivity parentActivity;
parentActivity = (YourRootActivity) this.getParent();
parentActivity.setTitle(title);

In parent define setTitle(String s) method

public void setTitle(String s){
 // set title here
}

TabActivity is deprecated ( http://developer.android.com/reference/android/app/TabActivity.html ). Please consider using something else:

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