Life of Fragment inside TabHost - android

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)?

Related

How to change the fragment inside the Fragment tab host

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;
}

Android FindFragmentByTag returns null in FragmentTabHost

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.

My onCreateView is called multiple times

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

How to use tabs in android after depreciation of tab activity? using fragment there is an issue with me

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.

How can I get the the fragment object in the current selected tab

I am using a Fragment tabs. each tab has a fragment in it in order to call it's reload method. how can I get the current selected fragment?
I need something like mTabHost.getFragment() but this method doesn't exist
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(
mTabHost.newTabSpec("tab1").setIndicator("Latest"), MainFragment.class,
getBundle(0));
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Today"), MainFragment.class,
getBundle(1));
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Week"), MainFragment.class,
getBundle(2));
mTabHost.addTab(mTabHost.newTabSpec("tab4").setIndicator("Month"), MainFragment.class,
getBundle(3));
mTabHost.addTab(mTabHost.newTabSpec("tab5").setIndicator("Year"), MainFragment.class,
getBundle(4));
How can I get the instance of MainFragment that the tab is using
I have tried this but i get a null pointer
MainFragment tag = (MainFragment)mTabHost.getTag();
tag.reload();

Categories

Resources