I have a method in Test1Activity that creates tabs inside a TabHost, but if I want have Test2Activity call the method, and I have to set the newTab method to static, I can’t use Intent because it utilizes “this” to set the class.
public class Test1Activity extends TabActivity {
public static void newTab(String tabIdentifier) {
TabHost.TabSpec spec; // Reusable TabSpec for each tab
Intent intent = new Intent().setClass(this, ChatActivity.class);
spec = Test1Activity.tabHost.newTabSpec(tabIdentifier).setIndicator(tabIdentifier, null).setContent(intent);
tabHost.addTab(spec);
modifyTabSize();
}
}
public class Test1Activity extends TabActivity {
Test1Activity.newTab("Joe");
}
How do I fix this so that it would work properly?
There are a couple of tricks to using a static
Context. One thing I have done occasionally is create a static Context and initialize it inonCreate. For example:Then, when you construct a new
Intentobject, usecontextinstead ofthis.