How to pass the data through tabs? - android

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.

Related

Refresh tab from another tab

I have an Activity that contains a ViewPager with four tabs .
each tab has a fragment.
When I click something in tab 4, I want tab 3 to refresh(or access a non-static method in tab 3 ) ..I've searched but all I found had this in it:
FragmentB fragment = (FragmentB)getFragmentManager().findFragmentByTag("FragmentB");
and I can't use it since I never set a tag on the fragment.
I also could call a function from fragment in another fragment but it has to be static , and I don't want that since everything inside it must be static too and that would mess things up for me..
is there a solution to this ?
First of all i think it's not a good practice to update a view which user cannot see on screen at the moment. It's much better to update your data when user navigates to screen. By the way it not our first concern now.
I'm not sure this is still a valid way to find Fragments in a ViewPager byTag but you can find them like below:
This method generates default Tag for a Fragment in ViewPager.
#NonNull
public static String generateViewPagerFragmentTag(int viewPagerId, int position) {
final StringBuilder tagBuilder = new StringBuilder();
tagBuilder.append("android:switcher:");
tagBuilder.append(viewPagerId);
tagBuilder.append(":");
tagBuilder.append(position);
return tagBuilder.toString();
}
Than you can use this tag to find your Fragment with findFragmentByTag method.
Also as mentioned in comments you can use an EventBus library to achieve what you want to do but be careful of Fragment Lifecycle states in ViewPager because the Fragment you want to communicate can be in onPause state an your changes canned be received. This depends on in which lifecycle method you subscribe and unsubscribe to bus. (If you are using OttoBus you can get no subscribers found for this event exception.) You can do same pattern with interfaces. You can create an interface and implement in your Fragments.
For an other solution, you can directly access our fragments in your ViewPager. Here's my answer for another question it.
Finally, as i mentioned at the beginning i think you should implement a solution which updates your data when user switched to specific tab of ViewPager. You can keep your data changes in a mem-cache and listen tab changes and update your view when user exactly navigated to that screen and ready to see data changes. You can check Repository pattern for a more complex solution.

Send Data Between two Tab - android

I have create Tabbed activity with two tab
fragment a Contain edittext and fragment b Contain listview ,and I want to send data from fragment a to fragment b ,Please add the code in full,
please help me.
Note : I do not want pass data to textview Iwant to listview
All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
Follow this link

Set a Tab from an activity

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.

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.

How to communicate between Android tabs

Im trying to setup some tabs for my android application, but i got stuck.
I cant find a way to communicate between the tabs..
I got 2 tabs.
|Search|Result|
The search tab is simply showing a TextEdit and a "Search" button.
Hitting the search button should make my app change to the result tab and display the result.
i have added the tabs as activities using new intents as
TabHost host=getTabHost();
host.addTab(host.newTabSpec("one")
.setIndicator("Search")
.setContent(new Intent(this, SearchTabActivity.class)));
host.addTab(host.newTabSpec("Result")
.setIndicator("Android")
.setContent(new Intent(this, ResultTabActivity.class)));
Now i cant find a way for SearchTabActivity to display ResultTabActivity and alter its view...
Im looking forward for any tip.
You definitely want to reconsider using Activities as the content of your tabs. The more standard approach is to use one Activity that uses Tabs to only show part of the layout when a particular tab is selected.
The Android documentation has an excellent worked example, check out Hello, TabWidget.
Alternative
If for some reason you do need to use Activities, you can pass information between them by either adding values to the extras bundle within the Intent your using to open each Activity, or by extending the Application class.
By extending the Application class (and implementing it as a Singleton) you get an object that will exist whenever any of your application components exist, providing a centralized place to store and transfer complex object data between application components.

Categories

Resources