Search code examples
androidandroid-activityandroid-tabhost

How to reference child activity from TabHost to call a public function?


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

Solution

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