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.
Related
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
I have an Activity that has a ViewPager with four Fragments. One of those Fragments must have two tabs inside of it so I tried FragmentTabHost.
private FragmentTabHost fragmentTabHost;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_zbritjet, container, false);
fragmentTabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
fragmentTabHost.setup(view.getContext(), getFragmentManager(), android.R.id.tabcontent);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("tab_ofertat").setIndicator("Ofertat"), OfertatItems.class, null);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("tab_bizneset").setIndicator("Bizneset"), BiznesetItems.class, null);
return view;
}
The problem is that android is throwing
java.lang.IllegalStateException: FragmentManager is already executing transactions
Stack trace:
android.support.v4.app.FragmentManagerImpl.ensureExecReady(FragmentManager.java:1946)
android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1992)
android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:762)
android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:289)
Try getChildFragmentManager() instead of getFragmentManager().
From the docs:
Return a private FragmentManager for placing and managing Fragments inside of this Fragment.
I am using FragmentAcitvity in my Tab Bar .
I am able to load a new fragment on tab changed.
But i have a problem Because i have many activties in one tab.
for example i have 2 tabs :
Tab1 , Tab2. and i have in
Tab1 : activityA-->activityB---ActivityC
and
Tab2: activityF
how i can achieve this.
my code is here.
Main Class
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
Context context=this;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(context, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(
mTabHost.newTabSpec("Records").setIndicator("")), ActiovityA.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("tab2"),acitvityf.class, null);
mTabHost.setCurrentTab(0);
}
}
my acitvityA is here
public class acitvityA extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.acitvitya_xml, container, false);
return v;
}
}
myactivityB is
public class acitvityb extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.acitvityb_xml, container, false);
return v;
}
}
how i can solve this ?what is missing here.
You might have to do a little bit of homework yourself, but i could give you a hint that could help you.
You have the tabs set well. What you could do is, When the transition from Activity1 to 2 takes place, hide the last fragment from the stack, and then show the Activity 2 fragment in tab one (here you will push the fragment into the stack and show it). Maintain a stack of the fragments you are using, and compare an instance of which fragment has been pushed or popped from the stack to bring back the correct fragment.
Its a combination of fragment transactions add, hide, and stack push and pop.
Stack is needed for back press events.
Hope that helps.
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'm having trouble getting a pointer to a Fragment which is the currently visible fragment in a FragmentTabhost.
I have a SherlockFragmentActivity called SecondActivity that loads the Tabhost from it's onCreate method like this:
if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
Fragment f = new TabsFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, f, "tabsfragment").commit();
}
TabsFragment is a SherlockFragment subclass with this onCreate method to create the tabs
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.layout.tabs);
mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("Offers",
getResources().getDrawable(R.drawable.offersale)),
OfferListFragment.class,
null);
mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("News",
getResources().getDrawable(R.drawable.newspaper)),
NewsFragment.class,
null);
return mTabHost;
}
Now when i'm in the 2nd tab, I have a background task done in a class that is initiated by the original activity SecondActivity, then I call this which is supposed to give me a reference to the tab, but it always returns null!
NewsFragment newsView = (NewsFragment) delegate.getSupportFragmentManager().findFragmentByTag("Tab2");
The delegate variable is a pointer back to SecondActivity when it starts the background class.
How do I get a pointer to the tab's fragment?
Am I wrong that "Tab2" set when adding the tabs is the Tag for the fragment?
I don't really like answering my own questions, but it's amazing what sleeping on it can do.
This monster gives me a pointer to the fragment in the tabhost
NewsFragment newsView = (NewsFragment) delegate
.getSupportFragmentManager()
.findFragmentByTag("tabsfragment")
.getChildFragmentManager()
.findFragmentByTag("Tab2");