Android: Tab switch - How to update display when switching between tabs? - android

The app I am building has a tabhost with three tabs. The goal is to update the display in the new tab when the switch takes place. I have the following tab change listener set up in the main activity that built the tab host.
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
public void onTabChanged(String tabId) {
//Now what?
}
});
The question is, How do I take the tabId given and use it to call a method in that tab?
Edit
To clarify a bit: when you create an Intent for a new tab you specify an Activity Class, an object of which, presumably, is created to handle setup and management of that tab.
intent = new Intent().setClass(this, Setup.class);
spec = tabHost.newTabSpec("setup").setIndicator("",
res.getDrawable(R.drawable.tab_setup))
.setContent(intent);
tabHost.addTab(spec);
What I am looking for with this question is how to get a reference to that object? In the example above, the Setup class is instantiated to handle the “Setup Tab”.
To restate my question now: How do I, from the OnTabChangeListener, call a method in the Setup class?

Try to use
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
public void onTabChanged(String tabId) {
//Now what?
Object.refreshDrawableState();
}
});
Where Object = any display Object.

As it turns out the solution to the problem is closer to home. Rather than attaching code to the TabHost, the solution involves getting into the Life-Cycle of the activity by overriding the onPause and onResume methods.
#Override
public void onResume() {
super.onResume();
// Update your UI here.
}

Related

android tab activity go to previous tab

I have an activity with five tabs. Everything looks okay when I go from tab 1 to tab 2 or tab 3. How can I go back programatically from tab 2 to tab 1?
Intent myIntent = new Intent(this, Tab1.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
This is not working properly because it starts activity 1 without any tab.
When going from tab 1 to tab 2 I can both see tab 1 and tab 2 (current tab activated). But when going from tab 2 to tab 1, both tab 1 and tab 2 disappear from the activity. What could cause this?
This will surely help you.
TabHost tabHost = (TabHost) getParent().findViewById(android.R.id.tabhost);
tabHost.setCurrentTab(1);
OR you can refer to this link
How to programmatically switch tabs using buttonclick in Android
Thanks :)
just use finish() method
public void onClick(View v)
{
finish();
startActivity(new Intent(Activity2.this, Activity1.class));
}
I don't know about the Intent.FLAG_ACTIVITY_CLEAR_TOP, never needed that, but the mentioned effect of loosing your tabs is produced by calling startActivity() from your TabHost, not one of your tabs. If that's the case, move the call there and your tabs should stay.
I have a similar situation but seems none of the answers help. so, I post my solution here:
// tab selection history, each tab has a tag which is a string
private List<String> tabIdHistory = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstanceState);
// this layout contains TabHost and TabWidget
setContentView(R.layout.activity_main);
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
#Override
public void onTabChanged(String tabId) {
tabIdHistory.remove(tabId); // ensure uniqueness
tabIdHistory.add(tabId);
}
});
// continue your tab initialisation, such as
// tabHost.addTab(tabHost.newTabSpec(TAG)
// .setContent(...).setIndicator(...));
}
#Override
public void onBackPressed() {
if (tabIdHistory.size() > 1) {
// pop the current last item, we want the second last
tabIdHistory.remove(tabIdHistory.size() - 1);
tabHost.setCurrentTabByTag(tabIdHistory.get(tabIdHistory.size() - 1));
} else {
super.onBackPressed();
}
}
If use select tab#1, tab#3, tab#2, tab#1, then the back stack is "3, 2, 1" and app will exit to main screen if user press back button three times. If you want to keep full history, comment out this line:
tabIdHistory.remove(tabId);

Clicking tab not showing Home activity

I have 3 tabs in my sample application with activity group. First tab contains search activity i.e.Home/Root activity and am displaying the results of search in another activity but under same tab i.e Tab1. When I press back button in result activity, it is going to search activity. Everything works fine till here. Now I want to go search activity by pressing tab1 instead of pressing back button. How can achieve this? I tried something like this
public class TabSample extends TabActivity {
public TabHost tabHost;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("OPT")
.setContent(new Intent(this, TabGroup1Activity.class)));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("EDIT")
.setContent(new Intent(this, TabGroup2Activity.class)));
tabHost.setCurrentTab(1);
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
public void onTabChanged(String arg0) {
if (tabHost.getCurrentTabTag().equals("tab1")) {
//What should I do to display search activity here
} else {
tabHost.setCurrentTab(1);
}
}
});
tabHost.setFocusable(true);
tabHost.requestFocus();
}
}
Can anyone please help let me know how to invoke search activity when tab is pressed? What will go into if part? Because if I use tabHost.setCurrentTab(index), it will display result activity but not search activity.
NOTE: I followed the tutorial given in this link.
I think what you want to do is this: when the 'tab1' tag is selected, go back to TabGroup1Activity if (and only if) the current activity is not that activity (basically you want to simulate a 'back' press).
If so, what you want is this:
if (getCurrentActivity().getClass() != TabGroup1Activity.class)
getCurrentActivity().finish()
I'm not 100% sure I understand you fully, but let's see :)
In your onTabChanged listener you can switch on which tab have been tabed, and then open the activity as normal inside an activitygroup:
public void onTabChanged(String tabId) {
if (tabId.contentEquals("tab1")) {
Intent intent = new Intent(tabHost.getContext(), TabGroup1Activity.class);
View view = StartGroup.group.getLocalActivityManager().startActivity("tab1", intent).getDecorView();
StartGroup.group.setContentView(view);
}
}
I just reviewed my code and think there's a bit more to explain here. The problem is that you don't stack activities as normal. Instead the workaround is to make a content stack and change these instead. So what I have done is to create a class StartGroup which extends
ButtonHandlerActivityGroup:
public class StartGroup extends ButtonHandlerActivityGroup {
// Keep this in a static variable to make it accessible for all the nested activities, lets them manipulate the view
public static StartGroup group;
// Need to keep track of the history if you want the back-button to work properly,
// don't use this if your activities requires a lot of memory.
private ArrayList<View> history;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.history = new ArrayList<View>();
group = this;
// Start the root activity within the group and get its view
View view = getLocalActivityManager().startActivity("UserList", new Intent(this, UserList.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();
replaceView(view);
}
public void back() {
if (history.size() > 0) {
// pop the last view
history.remove(history.size()-1);
setContentView(history.get(history.size()-1));
} else {
finish();
}
}
}
Then from the TabMaster class or what you call it you can use the StartGroup class to change the content view of an activity group.
This is something I wrote to work on devices from 2.2, so there might be an easier and more androidish way to accomplished it, but this works on almost all devices :)
Here is another thread where the use a similar approach:
Launching activities within a tab in Android
Let me know if I can help more.
There is an ArrayList in your ActivityGroup so override onPause() method in ActivityGroup and remove all the ids from ArrayList except the first one which must be your SearchActivity.
So when you go to other tab then comes back to SearchActivity( or on Tab1 ) Home will be displayed.

Android TabHost with only selected tab on stack

I have a TabHost with 4 tabs. I need only the selected tab activity to be available on the stack. When user changes the tab, how to finish the activity under previous tab. I tried the following code. Here showing code for first tab. It similar for remaining tabs:
spec = tabHost.newTabSpec("tab1").setIndicator("Tab1",
res.getDrawable(R.drawable.ic_tab_tab1))
.setContent(new Intent(this, Tab1.class)
.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP));
But the above code is deleting the Tab1 Activity on stack/heap only when the user comes again to that tab but not when user changes to new tab.
I've had a look at this, what is your reason for this?
How do you know what's on the stack? Are you depending on onDestroy() or something?
I haven't got a full answer but you can see which tab is active:
Let your class implement OnTabChangeListener
public class YourClass extends TabActivity implements OnTabChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Load all your normal objects as well as TabHost
// make your tabhost listen for tab changes
mTabHost.setOnTabChangedListener(this);
}
#Override
public void onTabChanged(String tabId) {
int currentTabNumber = mTabHost.getCurrentTab();
for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
{
if(i != currentTabNumber){
System.out.println("I'm not a currently active tab");
}
}
}
}

TabHost , TabSpec - onclick implementation

i am working with TabHost , TabSpec .i want to do some actions(like onClick) when TabSpec is clicked. has anyone tried this i am able to go to another activity with "home.setIndicator("Home")...." is it possible
Use http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html
Example
mTabHost.setOnTabChangedListener(new OnTabChangeListener(){
#Override
public void onTabChanged(String tabId) {
if(TAB_1.equals(tabId)) {
//Change first image
}
if(TAB_2.equals(tabId)) {
//chnage second image ...so on
}
}});
If you follow this tutorial, you'll see that it's possible to change the tab image (to show that the tab is being selected or not) by defining XML file in res/drawable/

Get index of selected tab in tabHost

I'm trying to store the index of the currently selected tab in onSaveInstanceState so I can restore it. However the getCurrentTab apparantely gives me back the String I used in the etTabHost().newTabSpec, which I find a bit weird since the documentation says it returns an int and setCurrentTab also taking an int.
Does anyone know how I can get the index of my currently selected tab so I can restore it?
you are on the right way, use setOnTabChangedListener to get your selected tab.
public class MainActivity extends TabActivity {
static TabHost mytabs;
mytabs = getTabHost();
mytabs.setOnTabChangedListener(new OnTabChangeListener() {
#Override
public void onTabChanged(String arg0) {
Log.i("***Selected Tab", "Im currently in tab with index::" + mytabs.getCurrentTab());
}
});
...
...
...
You can use getCurrentTab() that returns index of tab start from 0.
Use tabHost.getCurrentTab() to get Tab ...
tabHost= getTabHost();
tabHost.addTab(tab0); // TabSpec tab0=tabHost.newTabSpec(...
tabHost.addTab(tab1); // TabSpec tab1=tabHost.newTabSpec
int current = tabHost.getTabHost() ;

Categories

Resources