How can I create a Fragment with tabs on the top, hosting other Fragments?
I previously used android.support.v4.app.Fragment but switched to android.app.Fragment because of the PreferenceFragment class. My code was:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tabs,container, false);
mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Fragment B"),
FragmentB.class, null);
return rootView;
}
But this don't work anymore because setup require a v4-FragmentManager.
Any suggestion how to solve this problem?
Thanks in advance!
As #SubinSebastian answered here :
Add the following project as a library project to your application.
https://github.com/kolavar/android-support-v4-preferencefragment
This solution is better than all other solutions. Add this project as a library project to your workspace. You can keep everything including your fragment transaction as it is and when importing the PreferenceFragment do it like the following.
import android.support.v4.preference.PreferenceFragment;
instead of
import android.preference.PreferenceFragment;
Related
I am using a tabhost to hold and navigate multiple fragments. I am not sure what I am doing wrong but the tab is only showing the text but not the icon (I really only need the icon and no text). Fragment 1 and 2 tabs are setup to only show text but on the third fragment tab I try to add in the icon with getDrawable. There is no error everything compiles fine but it just doesn't show the icon.
Thanks for your help.
This is the start of my oncreateView method where I set the tabs up:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_my_discovered_songs, container, false);
mTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("fragment1").setIndicator("Fragment 1"),
OneFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragment2").setIndicator("Fragment 2"),
TwoFragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("fragment3").setIndicator("Fragment 3", getResources().getDrawable(R.drawable.com_facebook_button_icon)),
ThreeFragment.class, null);
I'm using TabLayout from Android Design Support Library in my app.
I setup tabs using viewPager in activity's onCreate and they work well.
viewPager.setAdapter(
new TabsAdapter(getSupportFragmentManager(),
new TabInfo("Test1", Fragment1.newInstance()),
new TabInfo("Test2", Fragment2.newInstance()),
new TabInfo("Test3", Fragment3.newInstance())
));
tabs.setupWithViewPager(viewPager);
But sometimes, randomly when screen goes to sleep and I unlock the phone (tested on Moto G 2014, Android Lollipop) tabs just disappear.
It's bad, because I can't reproduce it on purpose.
try using tabhost. It's a lot more stable. The code shown below is an example of a tabhost in a fragment with three child fragments. I use this kind of code myself and works perfectly
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mTabHost = new FragmentTabHost(getActivity());
mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.container);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("tab1"),
Tab1Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("tab2"),
Tab2Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("tab3"),
Tab3Fragment.class, null);
return mTabHost;
}
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.
I installed Eclipse on a new machine and it keeps creating fragment_main.xml when I create a new Android Application Project. How can I prevent Eclipse from doing this, since I don't need a fragment_main.xml? I'd rather only have the activity_main.xml on start.
1.Creat project normally.
2.Copy fragment_main.xml to activity_main.xml (content). Then delete fragment_main.xml
3.In MainActivity.java delete the following content :
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
and
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
enjoy it.
There's currently no way (as far as I know) to keep Android from creating a fragment when you create an Activity. You can uncheck the "Create Activity" check box and create a new Activity, but that's really all you can do.
Anyways, Fragments are good for Phone/Tablet Compatibility, and should be used in almost all scenarios except for very basic uses.
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.