Pass values from TabHost to TabEspec

Put your problem here if it does not fit any of the other categories.

Pass values from TabHost to TabEspec

Postby MFDonadeli » Thu Mar 08, 2012 7:02 pm

I have a TabHost with some TabSpecs (Activities). In my TabActivity I have a thread that is updating some values each 10 seconds and I'd like to pass these values to my currently shown Activity, and this Activity will execute a function each time this values get updated.

What the better way to do it? Implement a timer in the TabSpec Activity(visible activity), so in a period of time(using a timer) I get the values, or a listener that starts an event that warn the TabSpec(visible activity) the changes? Or there is another way?

Explaining what I want:

TabHost (TabActivity) => vars v1, v2, v3 updated by downloading some information from web.

Tabs: Tab1, Tab2, Tab3.

CurrentTab: Tab1, displays v1, v2 and v3(updating these values), and also executes a function when these values are updated

If I change the current tab to Tab2: Tab2, displays v1, v2 and v3(updating these values), and also executes a function when these values are updated

Thanks for help.
MFDonadeli
Freshman
Freshman
 
Posts: 2
Joined: Thu Mar 08, 2012 6:52 pm

Top

Re: Pass values from TabHost to TabEspec

Postby Phyll » Thu Mar 08, 2012 9:09 pm

Hi MFDonadeli,

I guess if I'm following what you are doing. You could have a daemon timer running in your main tabhost activity and showing the values for all three of your tabs. I don't know the relationship of the tab activities (if you are using an activity for each tab) but you can keep track of which activity is showing through the tabhost. Is that kind of the direction this is headed?

Look at the attached example project:

Phyll
Attachments
TabApp.zip
Tab Example
(57.46 KiB) Downloaded 50 times
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Re: Pass values from TabHost to TabEspec

Postby MFDonadeli » Thu Mar 08, 2012 9:43 pm

Thank you Phyll,

But in this example you are showing the value in the main layout (TabApp). My project is exactly the same, have a Intent for each tab.

What I want is the possibility of (Left, Center and Right) receives the value "count" and do your own job, for example for each value of count Left would show the message "Even" if count var is even and Right would show the multiplication table of the number, for example.
MFDonadeli
Freshman
Freshman
 
Posts: 2
Joined: Thu Mar 08, 2012 6:52 pm

Re: Pass values from TabHost to TabEspec

Postby Phyll » Thu Mar 08, 2012 10:54 pm

Hi MFDonadeli,

As you said, it is an example. But you are right that I was inferring the main activity could do most of the work, host the timer, get the data from the internet and make any calculations based on which tab was showing. If that's not the case or you need to pass information back and forth between the two you can do that with messages.

Here's something about that, uses a service as a relay point I guess. Once you bind to the service you can pass messages to the acivities with up to date data:

http://developer.android.com/reference/ ... rvice.html

You might also look into using a global variable (visible to the entire app) to store message handlers that would then be used to send messages between the activities. Has some good points and bad points.

I still like the first way.

Phyll
Phyll
Master Developer
Master Developer
 
Posts: 648
Joined: Fri Oct 14, 2011 11:19 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 13 guests