如何从 TabHost 引用子活动来调用公共函数? [英] How to reference child activity from TabHost to call a public function?

查看:24
本文介绍了如何从 TabHost 引用子活动来调用公共函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 TabHost,其中有两个子活动(在两个选项卡中).我还在这些活动之一中实现了一个公共功能,我想从我的父母 (TabHost) 调用它,以触发选项卡内的某些操作.

I have a TabHost with two child activities in it (in two tabs). I also implemented a public function in one of these activities that i would like to call from my parent (TabHost), to trigger some action within the tab.

是否可以从 TabHost 引用活动本身来调用公共函数?

Is it possible to reference the activity itself from the TabHost to call a public function?

谢谢

这是我的 tabhost 设置:

here is my tabhost setup:

    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);

推荐答案

我的方法是在扩展 BroadcastReceiver 的子活动中定义一个嵌套的侦听器"类.

My approach would be to define a nested 'listener' class in the child activity which extends BroadcastReceiver.

然后我会简单地从我的 TabActivity 广播一个 Intent,然后触发 BroadcastReceiver 来执行操作.

I would then simply broadcast an Intent from my TabActivity which would then trigger the BroadcastReceiver to perform the action.

给出示例代码...

步骤是...

  1. 在清单中定义意图过滤器
  2. 将嵌套的侦听器"添加到子活动
  3. 在子活动中设置 onResume()/onPause() 以注册/取消注册侦听器
  4. 在 TabActivity 中创建意图并在您希望孩子做某事时广播它

在 AndroidManifest.xml 中

In AndroidManifest.xml

<activity
    android:name=".MyActivity"
    android:label="@string/app_name"
    <intent-filter>
        <action android:name="com.mycompany.myApp.DO_SOMETHING" />
    </intent-filter>
</activity>

在 MyActivity.java 中

In 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
            }
        }
    }
}

在主 TabActivity 中

In the main TabActivity

private void MakeChildDoSomething() {

    Intent i = new Intent();
    i.setAction("com.mycompany.myApp.DO_SOMETHING");
    sendBroadcast(i);
}

这篇关于如何从 TabHost 引用子活动来调用公共函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆