How to access a method of an Activity inside TabActivity - android

I would like to access the public method of an Activity run by a TabActivity using:
TabHost tabHost = getTabHost();
Intent intentMsgsView = new Intent().setClass(this, TeamHuddleScreenMsgsView.class);
tabHost.addTab(tabHost.newTabSpec("msgs").setIndicator("Messages").setContent(intentMsgsView));
Basically, what I what is to let the parent Activity call a method in child Activity in order to do something. Is this possible?
I did try this:
((TeamHuddleScreenMsgsView)getTabHost().getCurrentTabView().getContext()).refreshModel();
but I'm encountering a ClassCastException. It seems that the getContext() still gives the TabActivity. Any help on how to get the child Activity?
Thanks.

Listen to what CommonsWare says here, I have gone this route and it was a horrible mistake, but here is how you do it.
((SportsActivity)getLocalActivityManager().getCurrentActivity()).setLayout(R.layout.helpview);
Here setLayout is a public method I made in SportsActivity

This will be much simpler if you would not put activities in your tabs and just put views in your tabs, as that will mean there is only one activity. Here is a sample project showing how this is done.
There is a getActivity() method on ActivityGroup (parent class of TabActivity) that is undocumented but might be what you want.

Related

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.

Android TabActivity instance in ViewGroup's chaild Activity

i have TabHost app with Three Activities,in first tab i created a viewGroup.every thing working fine
my problem is i want to get TabActivity instance from the view group's chaild Activity
How can i get this... i am trying like this in View group's child activity
Tab_Activity TabObj = (Tab_Activity)getParent();
abObj.someMethod();
i got Class Cast Exception,i think i am getting View group Activity instance,
i want to get TabActivity instance,please help me if any one knows answer
Tab_Activity TabObj = (Tab_Activity)getParent().getParent();
treid like this also...
Thanx in Advance
I just tested it: if your TabActivity properly extends the standard android.app.TabActivity then Tab_Activity TabObj = (Tab_Activity)getParent(); should work as designed.
EDIT: AH. You want to do something like this:
Tab_Activity TabObj = (Tab_Activity)((ChildActivity)getContext()).getParent();
from inside the ViewGroup.

Android: How to call function of Activity inside of tab from the tabhost

I have a tabhost with three tabs. Each is an activity. I would like to have a button which is in the action bar, the bar along the top with common buttons, call functions of the tab which is active.
For example, an add function which could add something different to each tab depending on what tab was present when you clicked the button.
So, I am aksing how to call a function in Activity A from the tabHost.
And if that wont work, perhaps I can update the database from the tabhost and then refresh the tab content. Would that be easier?
Thank you all for you time and support.
I used the following code within my TabActivity class to switch tab then call a public method defined in the activity of the tab:
getTabHost().setCurrentTab(0);
Activity MyActivity = this.getCurrentActivity();
MyActivity.myMethod();
Hopefully helpful to someone looking for the answer to this question.
Hi Just stumbled across this, not sure if you already found a solution?
I solved this myself recently. I was previously getting around the problem by raising a intent broadcast from the tabhost activity and receiving the broadcast within the sub tab activity. This worked for me but i was sure there is a "better" way.
A cleaner way is to achieve it with something like this:
might have something like this:
parentActivity - my "container"
activity which holds the TabHost
childActivity - my tab activity
which holds tab content and the
public method i want to call from
parentActivity
within parentActivity:
// a method used for onclick callback or whatever you need. within parentActivity (tabhost)
// this will get call huzzah() in the first tab - getChildAt(0)
onClick () {
childActivity childAct = (childActivity) getTabHost().getChildAt(0).getContext();
childAct.huzzah();
}
within childActivity:
// a public method for the parent activity to access
public void huzzah() {
Log.d("stuff", "huzzah() called");
}
Note: Another alternative i believe is to redesign to use views instead of activities in your tabs. This is a better overall alternative because IIRC memory wise you are only storing 1 activity on the stack rather than (n * tabs) number of activities
Hope that helps
Edited as per Peter O request:
I am on API 10, and this problem gave me a huge headache. I have 3 tabs, I want all of them to be aware of changes on the other. The problem I had was that once the activity for a tab is started, there seemed to be no call back so the activity understood the user switched to a different tab, and thus needed to do work to be sure its state was correct.
I found lots of answers to this problem, but none seemed to work.
The one that I finally got to work was the solution offered as #3 for this thread --but it too is confusing. I found that the getTabHost().setCurrentTab(0); does nothing; I implemented OnTabChangeListener() to call a function that used getTabHost().setCurrentTab(0); however, I found the getTabHost().setCurrentTab(0); caused the app to crash for any tab other than 0--e.g, If I chose tab B (index=1) then called getTabHost().setCurrentTab(1); the app crashed.
Using the Debugger, I found the call this.getCurrentActivity(); always returns the activity associated with the tab which the user clicked on--calling getTabHost().setCurrentTab(); did not change that fact, and caused the app to crash.
So I got rid of it and I can now call this.getCurrentActivity(), then call a method in the Actvitity class returned by that call --this lets the activity know it has to update it's state--in my case it does this using the application object.
The above way of calling the method will not work,
Here is the quick answer for the above problem:
getTabHost().setCurrentTab(0);
Activity myActivity=getCurrentActivity();
String name=((Tab1) myActivity).et1.getText().toString();
Here the above code is given in the onclick() method of the activity which has TahHost
where Tab1 is the secondactivity and et1 is the identity of the edittext in the Tab1 activity so you can get all the value of the different fields like this individually.

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.

Consistent tabs through out the application [duplicate]

Currently I have a TabHost implemented with 3 tabs each containing a separate activity. My question is how do I switch between tabs from within one of the activities that is located inside the tab host. I've looked everywhere and have been unsuccessful in finding a real answer to this problem.
After a long time of battling with this problem I've been able to find a solution to switching tabs when using activity based tabs.
In the parent activity class where the tabhost is created I implemented a method like the one below:
public void switchTab(int tab){
tabHost.setCurrentTab(tab);
}
Inside of the tab that I would like to be able to switch internally to another tab I created the method below:
public void switchTabInActivity(int indexTabToSwitchTo){
MintTrack parentActivity;
parentActivity = (MintTrack) this.getParent();
parentActivity.switchTab(indexTabToSwitchTo);
}
If you would like a good example of this code, you can take a look at my MintTrack project here and here.
As a side note, please be very careful when deciding whether to use view or activity based TabHost.
Activity based tabs are great because they can be separated into there own XML file. Activities can also be organized into there own Java file instead of being cluttered into one. That being said some of the things you would think would be easy become complicated with activity based tabs. Its hard to pass information between tabs without creating overhead. Activity based tabs also use more memory/CPU time as they have the overhead of the Activity around each of them. Please consider this and the many more trade offs before diving into using an Activity based TabHost. I know now that I would personally go with a view based TabHost if I ever used them again.
I encountered the same problem. While a single activity for all tabs would be better, sometimes taking the easy way out is the rational choice.
To avoid creating a new tab activity when a tab wants to change to another tab, I put this in my AndroidManifest.xml:
<activity android:name=".MyTabsActivity"
android:label="Tabs!"
android:launchMode="singleTask">
Send an intent with the tab you want:
class OneTabContentActivity {
void switchTab() {
final Intent intent = new Intent(mContext, MyTabsActivity.class);
intent.setAction("Switch to tab 1, please");
mContext.startActivity(intent);
}
class MyTabsActivity {
#Override
protected void onNewIntent (Intent intent) {
super.onNewIntent(intent);
getTabHost().setCurrentTab(1);
}
}
This solution has drawbacks but I'm not clear over the details. Someone else might know enough to point them out.
First, I set a method to my main class, which extends TabActivity let's call it "MainActivity"
public TabHost getMyTabHost() { return tabHost; }
Then, I add my tab activity class;
MainActivity ta = (MainActivity) this.getParent();
TabHost th = ta.getMyTabHost();
th.setCurrentTab(0);
It worked for me.
Step #1: Replace the tabs-holding-activities with tabs-holding-views by using a better form of setContent() on TabSpec
Step #2: Call setCurrentTab() on your TabHost from within your single Activity
I have yet to see any benefit to having an Activity be the content of a tab rather than a simple View. Having an Activity as the content of the tab wastes CPU time and memory (and, hence, battery life) and makes things like you're trying to do much more difficult.
I had a slightly different problem and thought I'd add this for anyone else facing a similar situation. I have an activity-based tabbed application and one of the tab activities launches another activity which is not controlled by the tabHost. I needed to have a button on this activity finish() (ie: return back to the main Tab view) and switch to a different tab at the same time.
I decided to handle it with a BroadcastReceiver. In the class that sets up the TabHost, I added this class:
class ChangeTabReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "ChangeTabReceiver: received");
TabHost tabHost = getTabHost();
tabHost.setCurrentTab(1);
}
}
..then defined the vars:
ChangeTabReceiver changeTabReceiver;
IntentFilter changeTabFilter;
..then added to onCreate():
changeTabReceiver = new ChangeTabReceiver();
changeTabFilter = new IntentFilter(myApplication.CHANGE_TAB);
registerReceiver(changeTabReceiver, changeTabFilter);
Finally in the new activity when you want to close that activity and switch the tabs, do this:
Intent intent = new Intent(myApplication.CHANGE_TAB);
this.sendBroadcast(intent);
this.finish();
Of course you could make a method to switch to various tabs by passing the tab index -- but in my case this behavior only occurs in one activity so I decided to keep it simple...
public void switchTab(int index){
MintTrack ParentActivity;
ParentActivity = (MintTrack) this.getParent();
ParentActivity.getTabHost().setCurrentTab(index);
}
I just put a public static TabHost tabHost;
in my TabActivity.
Then from any other tab I can do a MyTabActivity.tabHost.setCurrentTab(tabNumber);
Works fine for me (but I wish I'd used Fragments from the start.. I was just following the Tab tutorial in the Android documentation and working from there)

Categories

Resources