Set a Tab from an activity - android

I have a TabActivity and inside 5 tabs with 5 activities each one. I want to set one of my tabs but not from the TabActivity(clicking on the tab on the top) but from an activity of any tab, for example clicking on a button of activity 3(which is asociated with the 3rd tab).
In my TabActivity there is a variable mTabhost, which I can use to set the selected tab with
mTabHost.setCurrentTab(0);//home
I guess I need access to that variable from Activity1-2-3-4, and doing that variable static doesnt work.

Say you have a MyTabActivity as TabActivity, which hosts 5 Activity(ies).
If you want to change the Tabs inside SecondActivity, you would write the code something like this.
MyTabActivity myTabs = (MyTabActivity) this.getParent();
Here you have your MyTabActvity
you can change the tabs like:
myTabs.getTabHost().setCurrentTab(index);

Save this mHost var instance in singleton class say Utility class . Then access particular var from Utility Class in different activity and set it accordingly. It works. I 've used it in my several projects.

Related

How to call methods in 1st class to 2nd class with passing values

I have MainActivity and SecondActivity. in main activity implemented a viewpager and in my SecondActivity i have button. when i click on button in SecondActivity
move to main activity and jump to spacific viewpager page.
Please provide suitable examples.
In your code on button click write this line,
startActivity(new Intent(CurrentActivity Name, TargetActivity.class).putExtras(tempBundle)
tempBundle is a Bundle argument for identify which position of the pager you want to display at current.
Then in your target Activity get the Bundle information, and set your ViewPager.
viewPager.setCurrentItem(0); //You can set your Bundle information 0 or 1 or 3
That time your ViewPager can set according to given position.
We have Two ways
Use Static Methods OR
Use interface to communicate between activities and fragments also
Communication through Interface

Open another activity from inside the tab activity

I created a tabbed application in which I have three tabs.
In the first activity I am opening a class named Home.class and inside the Home.class I want to start another activity that should open in that tab itself is it possible.
Please help.
ActivityGroup is deprecated, try to use Fragment and FragmentManager
To start multiple Activity in one tab, you need to extend your tab with ActivityGroup. and when are you starting new Activity you need to use startChildActivity
Use Android ActivityGroup within TabHost to show different Activity

Removing a tab and the activity (intent) inside of it from a TabHost

I have an app that can create tabs dynamically. And when I create a tab I initiate an activity as an intent. Like so:
private void addTab(Context packageContext, Class<?> newClass, TabHost mTabHost, String tabId, String tabLabel){
// newClass is my Activity class that I want to start in the tab
Intent intent = new Intent().setClass(packageContext, newClass);
TabHost.TabSpec spec;
spec = mTabHost.newTabSpec(tabId).setIndicator(tabLabel)
.setContent(intent);
mTabHost.addTab(spec);
mTabHost.setCurrentTabByTag(tabId);
}
Pretty standard. And it works great. Now, suppose that I have a button (or menuitem, whatever) in the activity that I instantiated inside of my tab. When the user presses this button, I want the activity, and the tab it is inside of, to be removed and destroyed.
I can't seem to find a simple way to do this. I have found the TabHost.clearAllTabs() function, but this destroys all tabs and activities, I just want to remove one.
Someone suggested I save a list of all Tabs that I have opened, and then call clearAllTabs(), after which I recreate all of my other tabs except for the one I don't want.
Something like this:
public static ArrayList<TabHost.TabSpec> list = new ArrayList<TabHost.TabSpec>();
I add this line to my addTab() function so that every tab I create is remember in my ArrayList:
list.add(spec);
And then when I want to remove my tab I run this function:
public static void removeTab(){
list.remove(list.size()-1); // remove it from memory
mTabHost.clearAllTabs(); // clear all tabs from the tabhost
for(TabHost.TabSpec spec : list) // add all that you remember back
mTabHost.addTab(spec);
}
This removes my tab from my ArrayList, removes all tabs, then recreates all the tabs remaining using my ArrayList. In theory it should work, but I get the following error when I try call this function:
FATAL EXCEPTION: main
java.lang.NullPointerException
at android.widget.TabWidget.setCurrentTab(TabWidget.java:342)
at android.widget.TabWidget.focusCurrentTab(TabWidget.java:366)
at android.widget.TabHost.setCurrentTab(TabHost.java:323)
at android.widget.TabHost.addTab(TabHost.java:216)
at com.example.myapp.TabManager.removeTab(QuikBrowser.java:86)
at com.example.myapp.TabManager.TabWindow.onOptionsItemSelected(TabWindow.java:91)
at android.app.Activity.onMenuItemSelected(Activity.java:2205)
For some reason, when adding a tab, it attempts to set the current tab, and it hits a null pointer exception.
If you guys could suggest another way of achieving what I want to do, or a way to fix my current method, I would appreciate it.
Try changing current tab to 0.
Something like:
getTabHost().setCurrentTab(0);
getTabHost().clearAllTabs();
I was reading that calling clearAllTabs(); will throw a nullpointerexception if you don't set the tabhost to the first tab (.setCurrentTab(0)) before calling (.clearAllTabs())
Also this answer may help? (How to remove tab from TabHost)
I would suggest a different approach. You can use an ActivityGroup to build your own TabControl. As you are using normal Buttons (or similar controls just as you like) you can easyly arrange/create/remove them as needed.
I can't dump the whole code here but that is basically what I did when I had the same problem:
Create an Activity inherited from ActivityGroup
Place a ViewGroup in your layout where you want to show the sub-activities
Setup your buttons as needed (LinearLayout works fine with a variable count of buttons)
Start activites thru getLocalActivityManager().startActivity() as needed
You can now add/remove buttons as you like. The Activites follow the Android lifecycle so you don't have to delete them yourself.
You might have to implement onBackPressed on your ActivityGroup to properly handle the history but that depends on the project.

How to pass the data through tabs?

I have three tabs.. Personal info,profile info and FinalStep...
First of all i need to move to another tab using a button in one tab activity..
how to do it?
Secondly how to save data in these tabs... as i have a FinalStep tab which contains the final registration button...so i need to obtain data from the other tabs also(personal and profile)
How to do it?
I always save data as a public variable in the TabHost activity. You can access it via getParent().
You simple TabActivity-TabView combination for implementing this. While doing you will get a structure with 4 classes:
1) TabHostActivity: this will host your tabview.
2) Tab1Activity: this will be the view of first tab.
3) Tab2Activity and 4) Tab3Activity similarly will hold the view of tab2 and tab3.
Now for going to one activity to other use can use the TabHost variable used in TabHostActivity and set its currentTab function.
HelloTabWidget.tabHost.setCurrentTab(2);
And yes for saving the data, you can use public variables in TabHostActivity and use it as per your requirements.
For more details on how to use tabview, go to this link:
http://developer.android.com/resources/tutorials/views/hello-tabwidget.html
PS: this is a general idea of doing and yes, you can optimize it more as per your needs and requirements and this may not be the best method of doing this.

Android:How to change opened tab dynamically

I have an Android application which has four tabs (I use a main TabActivity with TabHost and TabSpecs).
In one of my sub activity (activity opened in a tab), i need to open a tab not by clicking on the tab title and i don't know how to do this.
For example, i have a button in my activity and when i click on it, it opens a different tab.
For the moment, it is what i do:
Intent intent = new Intent(myActivity.this, myTabActivity.class);
intent.putExtra("ComeFrom", true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Then in the TabActivity, if i get true reading the "ComeFrom" extra i open the wished tab but the problem is that it kills all the other activies. So, if someone knows a better (cleaner) way to do that trick, please tell me...
Found an easier (I think) answer:
on the TabActivity declare a public, static and self variable and populate it on the onCreate method. F.e.:
public class TheActivity extends TabActivity {
public static TheActivity self;
...
#Override
public void onCreate(Bundle savedInstanceState) {
self=this;
on any Activity running in a tab, when you want to change the one shown on your app. you can do this:
TabHost tabHost = TheActivity.self.getTabHost();
tabHost.setCurrentTab(0);
Worked ok for me, hope serves someone else!
You have to use TabHost's "setCurrentTab(...)" for that. In one of my projects, I created a static method in the main Activity (the one with the TabHost), named "swtichToTab(int tab)". In my subactivites (those inside the tabs) could then just call "MainActivity.switchToTab()" to trigger switching.
It may not be the cleanest method, I'm sure you can achieve this using broadcast intents too.
You can create a BroadcastReceiver and send a broadcast with the index of the tab as extra
You can use views instead of activities for the content of the tabs. This way, the code is simpler and doesn't use as much memory. Plus, you then can use the setCurrentTab(tabIndex) method to easily switch between views.
I have a simple tutorial here. It has a tab activity with a list and map view. When you you click on an item in the list, the activity dynamically goes to the map view (using the setCurrentTab(tabIndex) method). You can easily modify this to have a button switch views.

Categories

Resources