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

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

问题描述

我有两个孩子的活动TabHost它(两片)。我也是在这些活动中,我想从我的父母(TabHost)来调用,触发选项卡中的某些行为之一的执行公共职能。

是否有可能引用了活动本身从TabHost调用公共职能?

感谢

这是我的tabhost设置:

  RES = getResources();
    tabHost = getTabHost();

    TabHost.TabSpec规范;
    意向意图;

    意图=新的意图()setClass(这一点,home.class)。
    规格= tabHost.newTabSpec(家)setIndicator(组,res.getDrawable(R.drawable.groups))setContent(意向)。
    tabHost.addTab(规范);
    意图=新的意图()setClass(这一点,messages.class)。
    规格= tabHost.newTabSpec(信息)setIndicator(信息,res.getDrawable(R.drawable.messages))setContent(意向)。
    tabHost.addTab(规范);
 

解决方案

我的做法是将其延伸的BroadcastReceiver的子活动定义一个嵌套的监听类。

然后,我会简单地播放从我TabActivity一个Intent这将随后触发的BroadcastReceiver要执行的操作。

编辑::要举个例子code ...

的步骤...

  1. 定义的意图过滤器在清单
  2. 添加嵌套的听众的子活动
  3. 设置onResume()/的onPause()的子活动注册/注销监听器
  4. 在TabActivity创建意图和播放它时,你想要孩子做一些事情

在AndroidManifest.xml中

 <活动
    机器人:MyActivityNAME =
    机器人:标签=@字符串/ APP_NAME
    <意向滤光器>
        <作用机器人:名称=com.mycompany.myApp.DO_SOMETHING/>
    &所述; /意图滤光器>
< /活性GT;
 

在MyActivity.java

 公共类MyActivity延伸活动{

    私人myListener的侦听器= NULL;
    私人布尔MyListenerIsRegistered = FALSE;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreated(savedInstanceState);

        监听=新myListener的();
    }

    @覆盖
    保护无效onResume(){
        super.onResume();

        如果(!MyListenerIsRegistered){
            registerReceiver(监听器,新的IntentFilter(com.mycompany.myApp.DO_SOMETHING));
            MyListenerIsRegisterd = TRUE;
        }
    }

    @覆盖
    保护无效的onPause(){
        super.onPause();

        如果(MyListenerIsRegistered){
            unregisterReceiver(听众);
            MyListenerIsRegistered = FALSE;
        }
    }

    //嵌套的'听众'
    保护类myListener的扩展BroadcastReceiver的{

        @覆盖
        公共无效的onReceive(上下文的背景下,意图意图){

            //不需要检查,除非监听动作将
            //将处理超过一个 - 让我们做吧
            如果(intent.getAction()。等于(com.mycompany.myApp.DO_SOMETHING)){
                // 做一点事
            }
        }
    }
}
 

在主TabActivity

 私人无效MakeChildDoSomething(){

    意图I =新意图();
    i.setAction(com.mycompany.myApp.DO_SOMETHING);
    sendBroadcast(ⅰ);
}
 

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.

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

Thanks

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

解决方案

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

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

EDIT: To give example code...

The steps are...

  1. Define the intent filter in the manifest
  2. Add the nested 'listener' to the child activity
  3. Set onResume()/onPause() in child activity to register/unregister the listener
  4. Create intent in TabActivity and broadcast it when you want child to do something

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>

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

In the main TabActivity

private void MakeChildDoSomething() {

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

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

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