I have this piece of code for instantiating my Android app:
onCreate:
tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
//Adding all the tabs!
tabHost.addTab(tabHost.newTabSpec("Servers").setIndicator("Servers"),ServerTab.class, null);
tabHost.addTab(tabHost.newTabSpec("Shoutbox").setIndicator("Shoutbox"), ShoutBoxTab.class, null);
tabHost.addTab(tabHost.newTabSpec("Leader\nboards").setIndicator("Leader\nboards"), LeaderBoardsTab.class, null);
tabHost.addTab(tabHost.newTabSpec("About").setIndicator("About"), AboutTab.class, null);
tabHost.setCurrentTab(0);
When this is called, the onCreateView of the second tab (ShoutBox) is called four times. Resulting in a lot more loading time then needed. During the loading I didn't touch my phone or changed its state(Rotation etc). Why is this happening? Thanks
Related
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;
}
For example, I have 5 tab inside a fragment tabhost.
For my past experience, if I have to implement a "go to detail" page inside a tab, I would just simply use activity to do (e.g. intent and create new activity).
Since I would like to keep the tabhost bar at the page so I can not use activity anymore , I need to change the fragment which is inside the tabhost. I tried using fragment transaction but when I switch tab the text is overlap, so how to change the fragment inside the tab? Thanks a lot.
This is how I create tabhost
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
tabHost.addTab(
tabHost.newTabSpec("home").setIndicator("",
getResources().getDrawable(R.drawable.btn_home)),
HomeFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("exhibit").setIndicator("",
getResources().getDrawable(R.drawable.btn_exhibit)),
ExhibitFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("plan").setIndicator("",
getResources().getDrawable(R.drawable.btn_floor_plan)),
PlanFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("collection").setIndicator("",
getResources().getDrawable(R.drawable.btn_collection)),
CollectionFragment.class, null);
tabHost.addTab(
tabHost.newTabSpec("setting").setIndicator("",
getResources().getDrawable(R.drawable.btn_setting)),
SettingFragment.class, null);
This is how I go to detail before, I would like to use fragment now
private OnClickListener set_listener(final int pos) {
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ctx, CollectionDetail.class);
intent.putExtra("pos", pos);
startActivity(intent);
}
};
return listener;
}
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.
How to use tabs in android after depreciation of tab activity? by using sherlock and fragments i am able to do tabing but i want to set tab image as whole not tab icon...
i don't need black bg with my image i want my image on whole tab.
here is my code
private FragmentTabHost mTabHost;
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
// mTabHost.setup(this, getSupportFragmentManager(), R.id.tabFrameLayout);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("",
getResources().getDrawable(R.drawable.tab8)),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab2").setIndicator("Tab 2",
getResources().getDrawable(R.drawable.tab9)),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab3").setIndicator("Tab 3",
getResources().getDrawable(android.R.drawable.star_on)),
FragmentTab.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("tab4").setIndicator("Tab 4",
getResources().getDrawable(android.R.drawable.star_on)),
FragmentTab.class, null);
Help Required :)
I did it by using tabhost, you can use tabhost without tabactivity following is the code.
final TabHost tabHost=(TabHost)findViewById(R.id.tabhost);
tabHost.setup();
final TabSpec spec1 = tabHost.newTabSpec("Tab1");
View view = LayoutInflater.from(this).inflate(R.layout.tabbar8, tabHost.getTabWidget(), false);
spec1.setIndicator(view);
spec1.setContent(R.id.tab1);
where tabbar8 is layout which i want to set on my first tab
You can completely customize the ActionBar tabs by using setCustomView() on the ActionBar.Tab. You'll define an XML layout for the tab, then set similar to:
// Home tab
ActionBar.Tab tabHome = mActionBar.newTab();
tabHome.setText("Home");
LinearLayout layoutLinear = (LinearLayout)inflater.inflate(R.layout.layout_tab_standard, null);
TextView title = (TextView)layoutLinear.findViewById(R.id.title);
title.setText(strTabTitle);
tabHome.setCustomView(layoutLinear);
mActionBar.addTab(tabHome);
so you can put your ImageView, or set a background drawable, on the linear layout you define (R.layout.layout_tab_standard in this example).
You should also review the Action Bar Style Generator (http://jgilfelt.github.io/android-actionbarstylegenerator/), you might find this useful.
I am confused about moving between Fragments in a TabHost. I have following TabHost inside Activity:
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(),
R.id.layout_main_tab_content);
tabHost.addTab(tabHost.newTabSpec(FragmentA.TAG)
.setIndicator("first"), FragmentA.class, null);
tabHost.addTab(
tabHost.newTabSpec(FragmentB.TAG).setIndicator(
"second"), FragmentB.class, null);
If the tab in changed what exactly happens? What happens to the previous Fragment and the changed one? What is the transaction? Add/remove or replace?
Is there any way I can get (find) the Fragment from initialized tab? And how about restoring the previous fragment when I return to the tab (can I add the transaction to the backstack)?