2

I have an app with a TabHost and three tabs. He is an example of how I am creating each tab:

    intent = new Intent().setClass(this, Setup.class); //intent.getClass()
    spec = tabHost.newTabSpec("setup").setIndicator("",
                    res.getDrawable(R.drawable.tab_setup))
                    .setContent(intent);
    tabHost.addTab(spec);

My goal is, when you switch to a new tab, to call a method updating the information displayed on that tab.

Here is the question: How does a tab know it has been displayed?

codingCat
  • 2,396
  • 4
  • 21
  • 27

4 Answers4

7

Since you are using an Activity for each tab in your TabHost, you should use the Android lifecycle calls to update the information on your UI.

onResume is called every time you switch to another tab's Activity (onPause is then called in the last tab's activity). This will keep your code in the proper place, in case you ever decide to use a different UI to host these activities.

@Override
public void onResume() {
    super.onResume();
    // Update your UI here.
}
Austyn Mahoney
  • 11,398
  • 8
  • 64
  • 85
0

Override OnTabChangeListener in your activity class with the desired code.

Codeman
  • 12,157
  • 10
  • 53
  • 91
  • I believe the application lifecycle is used when running an activity in a tab also. So onPause and onResume would be great candidates for this too. – Austyn Mahoney Jul 27 '11 at 17:06
  • Austyn, This would also depend on whether they are using Tabs themselves, or fragments within a TabHost. I think the OnTabChangeListener is the safer way to go about this, but if they are using fragments your way may indeed be a bit more generalized, better way to handle this. Also don't forget to upvote answers you think are correct, people will not see them otherwise! :) – Codeman Jul 27 '11 at 17:20
  • I don't understand what you mean by "Tabs themselves". You can host a `View`, an `Activity`, or a `Fragment` in a TabHost. A `View` is the only thing that wouldn't use the lifecycle calls. Activity reuse would suggest placing the code to update the View in onResume, in case he ever wanted to move this code out of a TabHost. I also don't think that your answer is even correct. This method you linked to is in a TabHost widget, not an Activity. This needs to be overriden in your TabHost, which probably shouldn't be updating your activities Views. – Austyn Mahoney Jul 27 '11 at 17:29
  • Sorry, when I said that, I meant a TabHost using a View. I apologize that I wasn't more clear! – Codeman Jul 27 '11 at 17:42
  • And the winner is OnPause and OnResume. Thank you Austyn for the hits. :-) I have been able to detect the change with the TabChangeListener, but I am just enough of a newbie to android to be clueless about the life-cycle of an activity. Thanks again. – codingCat Jul 27 '11 at 17:54
  • Austyn, if you repost your solution as an answer rather than a comment, I will be more than happy to check it off to give you credit for the answer. – codingCat Jul 27 '11 at 17:58
  • Done, but I see you already figured that out. Always here to help! – Austyn Mahoney Aug 06 '11 at 00:18
0

onTabChangeListener works if the tab changes, if it does not (you click on the same tab) it won't get called, not sure if that is important or not to your application.

I suppose you could capture the tab click event if you want all events and not just changes. I think the reason the 2nd proposed solution is not working is they are not calling super.onClick(event) by the way.

Community
  • 1
  • 1
Idistic
  • 6,281
  • 2
  • 28
  • 38
  • This gives the machanics of when the tab changes occurs. This does not answer the question on how I get access to the class associated with the tab. Where do I find the reference to the tabs activity object. In the above example how do I get a reference to the Setup class? – codingCat Jul 27 '11 at 17:31
  • 1
    I dont see any reason why this answer is downvoted. Before you are trying to ask something provide relevant information in your case provide us with the "class associate with the tab". This answer is exactly how you should start doing what you want. I doubt people of this community gonna give you exact solution as you wanted, but giving you idea how to start or point you where you are wrong. – Nikola Despotoski Jul 27 '11 at 17:39
  • You don't. You should use the Activity's code to manage the Activity's UI, not its parent. The TabHost is only a container, not much code should go in it besides to add the individual activities. – Austyn Mahoney Jul 27 '11 at 17:39
  • @Austyn exactly, the UI element should only contain the required overrides with callbacks the Activity/controller – Codeman Jul 27 '11 at 17:43
  • #codingCat - that's not the question you asked, if you want a specific answer ask a specific question, do you really think people are going to interpret your inner thoughts, sheesh. – Idistic Jul 27 '11 at 19:12
  • Really? It felt like the question I asked. It may not have been exactly on topic; but then again, if I knew exactly what the topic was, I wouldn't have to ask the question. Thanks for taking to time to show how wrong I was though. Really. Thanks. – codingCat Jul 27 '11 at 19:38
0

I think the purpose of using tabs is to load a tab on the first click and then stay static until a user issues a refresh command. I would use a fresh button in the menu.

But if you need to update a tab every time it is clicked, then why not just use a few simple buttons? When they are clicked, you can swap your content areas easily.

  • Yes and no. Your solution would have been easy to implement. Most users however will appreciate it if the app updates itself. Also, by having the tab update itself, you keep all the code working on that tab local to the tab, making the resulting code more portable. – codingCat Jul 27 '11 at 18:41
  • Not really. In fact, most users do not prefer tabs updating themselves. That is why it is the default behavior of tabs in most tab implementations such as in Swing, VB, C#, Flex. iphone and many more. – sysCoding Jul 28 '11 at 00:33
  • I suppose that is a matter of opinion. I can say this, I have never seen an app on any platform where information is entered on one tab, and than requires user interaction to pull it into another tab. And IMHO requiring a click to do so would be counter-intuitive. – codingCat Jul 28 '11 at 03:47