How to call a particular Tab Activity from other activity - android

I have created 3 tabs using tabhost in the MaintabActivity;
TaboneActivity
TabtwoActivity
TabthreeActivity
there is a button in the TabtwoActivity, when i click on it , anohter activity called OutofServiceActivity.java is displayed.
my problem is when iam trying to call TabtwoActivity from OutofServiceActivity.java it will rediect to TabtwoActivity without showing the tab bar..?
what s the problem??

Give a call to TabHost.setCurrentTab(--tab id here--) in onCreate method when you set your activities.

Related

get nullpointer using getLocalActivityManager().getActivity(tag)

I'm using TabActivity to implement tabs in my project and I want to communicate with activities of tabs and invoke some methods from tabActivity and I use method below:
CoachActivity activity=(CoachActivity) getLocalActivityManager().getActivity(mTabHost.getCurrentTabTag());
but activity is null. CoachActivity is Tab1 activity.
My code is a bit long so I don't post it here. I double checked everything and I'm sure the problem isn't about the setup of my tabs. I set the current tab and I checked that CoachActivity is created.

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

android go back to a specific tab

ShareMarketActivity.java this handles the tabs, Tab1, Tab2, Tab3.java files..... which on load it load the 1st tab
in Tab3 (portfolio) when u click on the company it moves to another window, what i want to do is,when u click back button on the new window... it should again load the tabs and it moves to the Tab3.
back.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent("android.intent.action.ShareMarketActivity"));
}
});
So this will load the tabs again and show the tab1 as the starting tab. Not the Tab3. what i want is... ** load the ShateMarketActivity and straight away moves to tab3 ** ( in the 1st load i want Tab1 to be the 1st load not tab3)
You can use the setCurrentTab method of the tab host.
mTabHost.setCurrentTab(2);
What you need to do is listed briefly below. I am not putting any code though but it should be straightforward.
When you are navigating to a new window do not navigate through the child activities that reside in the TAB Host. Call getParent() and then start new Activity from the TabActivity which is the Parent.
Make the Tab launchmode in manifest as SingleTask.
You can not capture the result of the Activity in onActivityResult in TabHost and then call the child Activity to propogate the result by using getCurrentActivity()
The reason for the behavior you see is that Tabs are actually an ActivityGroup with multiple activities embedded in them for each TAB. Once you call a new Activity from any of the child Activity and then come back the Activity stack loses the Activity Group and lists the calling activity as parent and hence you just see TAB3 activity as the only activity.
Let me know if this does not help.

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.

Force Tabhost to initiate Activity (refresh)

To put it simply. From a context menu on a TabActivity how can I initiate executing the intent for the current tab? I am trying to force a refresh.
The tabs all initiate activities displaying a subset of people names. While in one list you call up an edit activity which allows you to associate the name to one of the other lists in the TabHost. Using the back button to get back to the tabhost (onResume fires) and the list has not updated. I would like to have a context menu item to refresh the current tab.
I know about using one activity for all the views in a tabhost but for many reasons I have not chosen that method.
You can also try adding a flag to the intent when you set up the tabs in the first place.
Intent i = new Intent().setClass(this, YourClass.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
TabHost.TabSpec spec = tabHost.newTabSpec("name")
.setIndicator("Class",res.getDrawable(R.drawable.ic_tab_something))
.setContent(intent);
Tabhost tabHost.addTab(spec);
I think due to the activity lifecycle, you will have some problems 'restarting' the activity (Activity lifecycle - startActivity()) - if you were to move any logic you have inside the onCreate method into another method, then call the method from onCreate and onResume, so it rebuilds the tab content for you.
Alternatively you could add a menu item to call this method, so onCreate calls the logic method on first run, and your user can call it from the menu to refresh the contents.

Categories

Resources