Android - How to update a display after a tab switch?
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); //inten开发者_如何学Got.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?
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.
}
Override OnTabChangeListener in your activity class with the desired code.
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.
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.
精彩评论