and*_*per 17 android android-tabhost android-activity
我有一个带有两个子活动的TabHost(在两个标签中).我还在其中一个活动中实现了一个公共函数,我想从我的父母(TabHost)调用,以触发选项卡中的某些操作.
是否可以从TabHost引用活动本身来调用公共函数?
谢谢
这是我的tabhost设置:
res = getResources();
tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, home.class);
spec = tabHost.newTabSpec("home").setIndicator("Groups", res.getDrawable(R.drawable.groups)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, messages.class);
spec = tabHost.newTabSpec("messages").setIndicator("Messages", res.getDrawable(R.drawable.messages)).setContent(intent);
tabHost.addTab(spec);
Run Code Online (Sandbox Code Playgroud)
Squ*_*onk 22
我的方法是在扩展BroadcastReceiver的子活动中定义一个嵌套的"监听器"类.
然后我会简单地从我的TabActivity广播一个Intent,然后触发BroadcastReceiver来执行动作.
编辑:给出示例代码......
步骤是......
在AndroidManifest.xml中
<activity
android:name=".MyActivity"
android:label="@string/app_name"
<intent-filter>
<action android:name="com.mycompany.myApp.DO_SOMETHING" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
在MyActivity.java中
public class MyActivity extends Activity {
private MyListener listener = null;
private Boolean MyListenerIsRegistered = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreated(savedInstanceState);
listener = new MyListener();
}
@Override
protected void onResume() {
super.onResume();
if (!MyListenerIsRegistered) {
registerReceiver(listener, new IntentFilter("com.mycompany.myApp.DO_SOMETHING"));
MyListenerIsRegisterd = true;
}
}
@Override
protected void onPause() {
super.onPause();
if (MyListenerIsRegistered) {
unregisterReceiver(listener);
MyListenerIsRegistered = false;
}
}
// Nested 'listener'
protected class MyListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// No need to check for the action unless the listener will
// will handle more than one - let's do it anyway
if (intent.getAction().equals("com.mycompany.myApp.DO_SOMETHING")) {
// Do something
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在主TabActivity中
private void MakeChildDoSomething() {
Intent i = new Intent();
i.setAction("com.mycompany.myApp.DO_SOMETHING");
sendBroadcast(i);
}
Run Code Online (Sandbox Code Playgroud)
Moy*_*she 16
我找到了另一种可能更简单的解决方案.我相信OP不再需要这个,但也许未来的某个人会很高兴找到它.
所以,基本上,在你的孩子的活动运行的公共方法,你只需要这小小的一段代码在你的父母(tabHost,home并message从OP的TabHost配置采取):
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
Activity currentActivity = getCurrentActivity();
if (currentActivity instanceof home) {
((home) currentActivity).aPublicMethodFromClassHome();
}
if (currentActivity instanceof messages) {
((messages) currentActivity).aPublicMethodFromClassMessages();
}
}
});
Run Code Online (Sandbox Code Playgroud)
我在我的应用程序中使用它.作为魅力;)
我正在调试一个Android应用程序,并遇到了同样的需求.我通过以下代码片段找到了一个非常直截了当的答案:
String currentActivityId = getLocalActivityManager().getCurrentId();
Activity currentActivity = getLocalActivityManager().getActivity(currentActivityId);
Run Code Online (Sandbox Code Playgroud)
这里的标识符是创建TabSpec时给出的标识符:
tabHost.newTabSpec("id").setIndicator(indicator).setContent(intent);
Run Code Online (Sandbox Code Playgroud)
谢谢,这帮助我解决了一个类似的问题!
Activity currentActivity = getLocalActivityManager().getActivity(_TabHost.getCurrentTabTag());
if(currentActivity != null && currentActivity instanceof iMyActivity)
{
// pass to children
((iMyActivity)currentActivity).onLaunchDelegate();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14764 次 |
| 最近记录: |