Android: How to call function of Activity inside of tab from the tabhost
Asked Answered
H

4

7

I have a tabhost with three tabs. Each is an activity. I would like to have a button which is in the action bar, the bar along the top with common buttons, call functions of the tab which is active.

For example, an add function which could add something different to each tab depending on what tab was present when you clicked the button.

So, I am aksing how to call a function in Activity A from the tabHost.

And if that wont work, perhaps I can update the database from the tabhost and then refresh the tab content. Would that be easier?

Thank you all for you time and support.

Hie answered 16/3, 2011 at 0:24 Comment(0)
A
7

Hi Just stumbled across this, not sure if you already found a solution?

I solved this myself recently. I was previously getting around the problem by raising a intent broadcast from the tabhost activity and receiving the broadcast within the sub tab activity. This worked for me but i was sure there is a "better" way.

A cleaner way is to achieve it with something like this:

might have something like this:

  • parentActivity - my "container" activity which holds the TabHost
  • childActivity - my tab activity which holds tab content and the public method i want to call from parentActivity

within parentActivity:

// a method used for onclick callback or whatever you need. within parentActivity (tabhost)
// this will get call huzzah() in the first tab - getChildAt(0)
onClick () {
  childActivity childAct = (childActivity) getTabHost().getChildAt(0).getContext();
  childAct.huzzah();
}

within childActivity:

// a public method for the parent activity to access
public void huzzah() {
  Log.d("stuff", "huzzah() called");
}

Note: Another alternative i believe is to redesign to use views instead of activities in your tabs. This is a better overall alternative because IIRC memory wise you are only storing 1 activity on the stack rather than (n * tabs) number of activities

Hope that helps

Abuttal answered 28/5, 2011 at 5:45 Comment(3)
Hi wired00, I attempted to implement this solution but am getting a ClassCastException. I've essentially copied your code, but here is a copy of mine just in case: SearchActivity childAct = (SearchActivity) getTabHost().getChildAt(0).getContext(); childAct.changeStateTo(1); In this code, SearchActivity is the name of the class, and changeStateTo() is the public method being called. Any assistance?Irresolution
onClick is wrong code. Return no child activity, but child view of tabhost - LinearLayoutShipwright
Hi @Irresolution , i am facing the same problem.did u get any solution??Dd
T
14

I used the following code within my TabActivity class to switch tab then call a public method defined in the activity of the tab:

getTabHost().setCurrentTab(0);
Activity MyActivity = this.getCurrentActivity();
MyActivity.myMethod();

Hopefully helpful to someone looking for the answer to this question.

Trisomic answered 24/6, 2011 at 14:52 Comment(1)
@dan k : hi I create 3 intents of activities(which i add to TabHost) in my main TabActivity. I also have refresh in tabhost layout, on click of this refresh button i want to refresh list of intent activity/currrent tab activity, normally public method is called like u said, but in other case it shows conext problem...any idea over this?Fiberglass
A
7

Hi Just stumbled across this, not sure if you already found a solution?

I solved this myself recently. I was previously getting around the problem by raising a intent broadcast from the tabhost activity and receiving the broadcast within the sub tab activity. This worked for me but i was sure there is a "better" way.

A cleaner way is to achieve it with something like this:

might have something like this:

  • parentActivity - my "container" activity which holds the TabHost
  • childActivity - my tab activity which holds tab content and the public method i want to call from parentActivity

within parentActivity:

// a method used for onclick callback or whatever you need. within parentActivity (tabhost)
// this will get call huzzah() in the first tab - getChildAt(0)
onClick () {
  childActivity childAct = (childActivity) getTabHost().getChildAt(0).getContext();
  childAct.huzzah();
}

within childActivity:

// a public method for the parent activity to access
public void huzzah() {
  Log.d("stuff", "huzzah() called");
}

Note: Another alternative i believe is to redesign to use views instead of activities in your tabs. This is a better overall alternative because IIRC memory wise you are only storing 1 activity on the stack rather than (n * tabs) number of activities

Hope that helps

Abuttal answered 28/5, 2011 at 5:45 Comment(3)
Hi wired00, I attempted to implement this solution but am getting a ClassCastException. I've essentially copied your code, but here is a copy of mine just in case: SearchActivity childAct = (SearchActivity) getTabHost().getChildAt(0).getContext(); childAct.changeStateTo(1); In this code, SearchActivity is the name of the class, and changeStateTo() is the public method being called. Any assistance?Irresolution
onClick is wrong code. Return no child activity, but child view of tabhost - LinearLayoutShipwright
Hi @Irresolution , i am facing the same problem.did u get any solution??Dd
M
0

Edited as per Peter O request:

I am on API 10, and this problem gave me a huge headache. I have 3 tabs, I want all of them to be aware of changes on the other. The problem I had was that once the activity for a tab is started, there seemed to be no call back so the activity understood the user switched to a different tab, and thus needed to do work to be sure its state was correct.

I found lots of answers to this problem, but none seemed to work.

The one that I finally got to work was the solution offered as #3 for this thread --but it too is confusing. I found that the getTabHost().setCurrentTab(0); does nothing; I implemented OnTabChangeListener() to call a function that used getTabHost().setCurrentTab(0); however, I found the getTabHost().setCurrentTab(0); caused the app to crash for any tab other than 0--e.g, If I chose tab B (index=1) then called getTabHost().setCurrentTab(1); the app crashed.

Using the Debugger, I found the call this.getCurrentActivity(); always returns the activity associated with the tab which the user clicked on--calling getTabHost().setCurrentTab(); did not change that fact, and caused the app to crash.

So I got rid of it and I can now call this.getCurrentActivity(), then call a method in the Actvitity class returned by that call --this lets the activity know it has to update it's state--in my case it does this using the application object.

Mitchmitchael answered 14/3, 2012 at 20:36 Comment(2)
This answer is a little confusing. Can you please format it and edit it so it stands alone as an answer better? For instance, what is "the above code?" (Answers can be sorted in a different order, so that referring to an answer "above" may be incorrect.)Feeling
I agree with Peter. Welcome to SO Derwood and thank you for answering but learning formatting will help you get accepted more and get better feedback. As for errors on tabs other than 0... Sounds like something else is causing the error.Hie
H
0

The above way of calling the method will not work,

Here is the quick answer for the above problem:

getTabHost().setCurrentTab(0);

Activity myActivity=getCurrentActivity();

String name=((Tab1) myActivity).et1.getText().toString();

Here the above code is given in the onclick() method of the activity which has TahHost where Tab1 is the secondactivity and et1 is the identity of the edittext in the Tab1 activity so you can get all the value of the different fields like this individually.

Hypersthene answered 24/2, 2014 at 11:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.