I need to pass data between tab fragments after calling switch method of tabhost mTabHost.setCurrentTab(index);
public class FragmentTabs extends FragmentActivity {
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_tabs);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.content);
mTabHost.addTab(mTabHost.newTabSpec("class1").setIndicator("Class 1"),
Class1.class, null);
mTabHost.addTab(mTabHost.newTabSpec("class2").setIndicator("Class 2"),
Class2.class, null);
mTabHost.addTab(mTabHost.newTabSpec("class3").setIndicator("Class C"),
Class3.class, null);
}
}
Is there any way to do that ?
Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.
Check this: https://developer.android.com/training/basics/fragments/communicating.html
Related
I have a Fragment which implements FragmentTabHost and creates a couple of tabs each of which has a Fragment. I am trying to get a reference to one of these Fragments, but am drawing a blank as to how.
public class TabFragment extends Fragment {
private FragmentTabHost mTabHost;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.container);
addTab("study","Study",StudyFragment.class);
addTab("tab2","Tab 2",Tab2Fragment.class);
return mTabHost;
}
private <T extends Fragment> void addTab(String sTag, String sTitle, Class<T> c) {
View tabView = createTabView(getActivity(),sTitle);
mTabHost.addTab(mTabHost.newTabSpec(sTag).setIndicator(tabView),
c, null);
}
public StudyFragment getStudyFragment() {
// not working as fragment has not been tagged
return getChildFragmentManager().findFragmentByTag("study");
}
So the question is how do I either tag the study fragment or return a reference to it when it is created?
I've seen a couple of solutions which suggest overriding onAttachFragment, but that is for FragmentActivity not Fragment
To get a fragment, you can set an id to your fragment, then use this:
FragmentManager manager = getSupportFragmentManager();
MyFragment myFragment = (MyFragment)manager.findFragmentById(R.id.MyFragment);
Ok so you have to add tabs using TabHost.TabSpec like that:
tabHost = (FragmentTabHost)mainView.findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
TabHost.TabSpec tabOne = tabHost.newTabSpec("One").setContent(R.id.fragOne);
TabHost.TabSpec tabTwo = tabHost.newTabSpec("Two").setContent(R.id.fragTwo);
tabHost.addTab(tabOne, TabOne.class, savedInstanceState);
tabHost.addTab(tabTwo, TabTwo.class, savedInstanceState);
In this code R.id.fragOne and R.id.fragTwo refers to two FrameLayout defined in the activity layout.
If you dont have xml layout you can define programmatically a new FrameLayout with an id for each tab.
I am trying to retrieve a fragment from my fragmentTabHost:
private void initView() {
FragmentTabHost mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("summary").setIndicator("Summary"),
SummaryActivity.class, bundle);
SummaryActivity summaryActivity = (SummaryActivity) getSupportFragmentManager().findFragmentByTag("summary");
System.out.println(summaryActivity);
}
When I try to get my fragment immediately after it was created, it returns null. It could be a thread issue. How, or even when, should I call getFragmentByTag to retrieve my fragment in this case?
The Fragment you are looking for is not in the stack. It is not even added/present at the time you are finding it back.
findFragmentByTag method will look and match last fragment that added in the transaction, if that is not matching with the required one, then it will search with the available list from history. Finally if your tag is not there with any fragments it will return null.
Make sure that , you are adding the Fragment to the view first.
the view should receive the fragment/tabhost to make it available in the lifecycle.
you have to return your tabHost to the view to add the fragment before you do findFragmentByTag lookup.
For ex:
You are in your Fragment's onCreateView then you should do return the tabhost to the view after you inflated it.
onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.fragment1);
mTabHost.addTab(mTabHost.newTabSpec("fragmenttag").setIndicator("Fragmenttag"),
FragmentStackSupport.CountingFragment.class, null);
return mTabHost;
}
Then probably your Fragment will be available in back stack , you can get it back by Tag.
Fragment Tags can only be set during fragment transactions. The tab host tag refers to something else. Try using findFragmentById() if you set an id on the fragments root view.
Here, i am using tab widget and having 3 tabs tab1, tab2 and tab3.I am setting fragments on each tab,like Fragment1 will set initially on first tab.I move on to different fragment from Fragment1 on a button click and then when i clicked on first tab not able to set Fragment1 as initailly was set.
Here is my code to set fragment initially on tabs:
public class MainActivity extends FragmentActivity{
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_layout);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("tab1", getResources().getDrawable(R.drawable.list)),
ListOfArticlesFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("tab2", getResources().getDrawable(R.drawable.picture)),
PicturesFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("tab2", getResources().getDrawable(R.drawable.calendar)),
ListOfArticlesFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("tab4", getResources().getDrawable(R.drawable.help)),
ListOfArticlesFragment.class, null);
}
When i move from ListOfArticlesFragment to another fragment on a button click, how to set
ListOfArticlesFragment again when clicked on tab1.
Thanks in advance.
I have a MainActivity (FragmentActivity) that has a FragmentTabHost.
public class FragmentTabs extends FragmentActivity {
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_tabs);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("classA").setIndicator("Class A"),
ClassA.class, null);
mTabHost.addTab(mTabHost.newTabSpec("classB").setIndicator("Class B"),
ClassB.class, null);
mTabHost.addTab(mTabHost.newTabSpec("classC").setIndicator("Class C"),
ClassC.class, null);
}
}
ClassA, ClassB and ClassC are all Fragments (android.support.v4.app.Fragment).
I need to pass data (and call methods) on the Fragments. How can I get a reference of each of the Fragments, like this:
ClassA mClassAFragment = ???;
I’ve tried using getSupportFragmentManager().findFragmentByTag() and I’ve also tried exploring the capabilities of mTabHost. Nothing can get them.
Can you suggest a way to do this or suggest an alternative approach?
And i tried used this...
Bundle arguments = new Bundle();
arguments.putString("some id string", "your data");
YourFragment fragment = new YourFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction().add(R.id.fragmentid, fragment).commit();
but i dont know how can use this R.id.fragmentid if my add tabs is well:
mTabHost.addTab(mTabHost.newTabSpec("classA").setIndicator("Class A"),
ClassA.class, null);
Anyone help me?
Define the Fragments in your Activity as public. Then in each Fragment you also reference the Activity.
In your Activity:
public ClassAFragment aFragment;
...
// during initialization:
aFragment = new ClassAFragment();
// or you get it from your TabHost
FragmentManager fm = getFragmentManager();
aFragment = fm.findFragmentByTag("ClassA");
aFragment.mActivity = this;
// same with B and C
In your Fragments:
public MainActivity mActivity;
...
// in Fragment C
String newText = mActivity.aFragment.editText.getText().toString() + mActivity.bFragment.editText.getText().toString();
textView.setText(newText);
I am creating a Cash Sales app where I will implement 4 tabs. From 1st tab user will select customer from customer list, 2nd tab to select item from item list, 3rd tab to set payment details in 7 EditText's and 4th to view draft and confirm to save in SQLite. I have couple of questions on it:
For tab, should I create a tab container first by extending FragmentActivity like following:
public class CashSales extends FragmentActivity {
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cash_sales_tab);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("customer").setIndicator("customer"),
CustomerSelect.class, null);
mTabHost.addTab(mTabHost.newTabSpec("item").setIndicator("item"),
ItemSelect.class, null);
mTabHost.addTab(mTabHost.newTabSpec("payment").setIndicator("payment"),
SetPayment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("payment").setIndicator("payment"),
DraftViewAndSave.class, null);
}
}
Should I create different classes for each of the activities like CustomerSelect, ItemSelect etc? If need to create different classes should it be extended from Fragment class or FragmentActivity class?
How can I memorize the data when user will go from 1st tab to 2nd tab? Should I use Session and finally save data from session to database?
Guys I am new in Android. Please help me on this or send any sample link.
I've implemented the same thing so I am sharing my thoughts.
For tab, should I create a tab container first by extending FragmentActivity
You should create a class which extend the FragmentActivity . But rather using tabs I used ViewPager and custome FragmentPagerAdapter which hold your four different Fragnment (in your case CustomerSelect,ItemSelect etc) and on swipe You can save the data in Bundle in your Fragments and then call a public method from your FragmentActivity to get Bundle Object from your fragment classes
Should I create different classes for each of the activities like CustomerSelect, ItemSelect etc?
Yes you should.
How can I memorize the data when user will go from 1st tab to 2nd tab? Should I use Session and finally save data from session to database?
As describe above you can save the data in bundle object and then in your FragmentActivity you can call your save method in onPageSelected method like .
private Bundle firstFragmentData;
mPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
switch (arg0) {
case 1:
fragment1 = (MyFirstFragment) getSupportFragmentManager()
.findFragmentByTag(
"android:switcher:" + R.id.pager + ":"
+ (arg0 - 1));
firstFragmentData = fragment1.SaveDatainFragment1();
break;
Actually you need to set the getters for your all bundle objects which you get from different fragments in your main FragmentActivity like
public Bundle getFirstFragmentData() {
return firstFragmentData;
}
Now In your any Fragment you can get any Fragment data like this..
Bundle firstFragmentData = ((MainFragmentActivity) getActivity())
.getFirstFragmentData(); // here you got the bundle
I hope this helps.