I have created a application. Following is scenario before I explain problem.
I have Activity A which has multiple fragments such as F1,F2,F3 etc.
Now for F1 fragment I have implemented FragmentTabHost with three Fragments F11,F12,F13 fragment views. On tab is working fine for this.
But today i noticed one problem.
Say I am inside F1 I show three fragment tabs F11,F12,F13. User can switch between tabs and it works fine.
Problem is say i goto Fragment F13 from F11 by pressing tab. It shows F13 fragment successfully.
However when I click Back Button on menu it goes back F11 fragment but empty screen is shown means F11 view is not shown..
This is my F1 fragment code implementing FragmentTabHost:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.home_fragment, container,
false);
setHasOptionsMenu(true);
// realtabcontent = (FrameLayout) rootView
// .findViewById(R.id.realtabcontent);
mTabHost = (FragmentTabHost) rootView
.findViewById(android.R.id.tabhost);
mTabHost.setup(getActivity(), getChildFragmentManager(),
R.layout.home_fragment);
mTabHost.setOnTabChangedListener(this);
View tabView = createTabView(getActivity(), "Featured");
spec = mTabHost.newTabSpec("featured").setIndicator(tabView);
mTabHost.addTab(spec, FeaturedHomeTab.class, null);
tabView = createTabView(getActivity(), "Top");
spec = mTabHost.newTabSpec("top").setIndicator(tabView);
mTabHost.addTab(spec, TopHomeTab.class, null);
tabView = createTabView(getActivity(), "New");
spec = mTabHost.newTabSpec("new").setIndicator(tabView);
mTabHost.addTab(spec, NewHomeTab.class, null);
onTabChanged("featured");
return rootView;
}
So this is main code with three fragments and when i back from one fragment to previous fragment in tabhost view disappears.
What can be problem. Please help.
Tabs aren't meant to participate in temporal navigation (which is what back nav is for) because they represent content at same level of hierarchy.
In case of fragments, the back nav usually pops the back stack. There is a caveat that back nav doesn't pops sub (child) fragments first. So, the fragments added to Activity are removed on back, this includes the entire tabs fragment (along with its child fragments).
Related
I have a Viewpager with two fragments.
I want to replace each of the fragment with 3 different fragments.
How can I do this?
Also, can I change whole ArrayList with the new list of fragments after button click in the current fragment?
Here is what I've done.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_healthy_mode, container, false);
swipeLeft = v.findViewById(R.id.leftSlide);
swipeRight = v.findViewById(R.id.rightSlide);
pageArray = new ArrayList<Fragment>();
pageArray.add(new Female());
pageArray.add(new Male());
healthyModeAdapter = new HealthyModeAdapter(getFragmentManager(), pageArray);
viewPager = (ViewPager) v.findViewById(R.id.healthyModeViewPager);
viewPager.setAdapter(healthyModeAdapter);
// I want to replace female n male fragment with
pageArray.add(new EctoBodyFemale());
pageArray.add(new EndoBodyFemale());
pageArray.add(new MesoBodyFemale());
pageArray.add(new EctoBodyMale());
pageArray.add(new EndoBodyMale());
pageArray.add(new MesoBodyMale());
ViewPager supports operation with Views or Fragments if I understand correctly you want each page each 3 sub components that are static, in such case you have 2 options:
Using nested fragments, create a Fragment for each page that will contain your sub-fragments. and use these parent fragments as the fragments your provide to the pager view
Note that nested fragments are not recommended, they have been proven to be buggy and might affect performances. So I suggest you to take the View approach, create an xml that defines the fragments, and after you inflate it provide it directly to the ViewPager.
I am currently working on an application with ActionBar tabs inside one of its fragments (main navigation is NavigationDrawer). The fragment's onCreate():
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
actionBar = activity.getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Resources r = getResources();
//(…) tabNames initialization
adapter = new CustomTabAdapter(getFragmentManager());
}
and onCreateView():
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment, container, false);
viewPager = (ViewPager) rootView.findViewById(R.id.pager);
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(this);
for (String tabName : tabNames) {
actionBar.addTab(actionBar.newTab()
.setText(tabName)
.setTabListener(this));
}
return rootView;
}
There are 4 tabs, each of them with its own Fragment and layout. CustomTabAdapter returns new instance of proper fragment in its getItem(int). There are 4 tabs.
And now my problem is:
I start the application.
I choose this tab fragment from NavigationDrawer list. Everything is working just fine.
I choose another fragment from NavigationDrawer list which means that tab fragment is replaced with it.
I choose tab fragment again. And fragment related to tab that was selected before choosing another fragment from NavigationDrawer list, and one's adjacent to it are not recreated (blank screen under ActionBar tabs). I checked and onCreateView(…) methods of those fragment are not called. So after changing device orientation, or choosing tab not adjacent and this one again proper layout is shown.
How can I make it work as it should (showing proper layout on reentering tab fragment from NavigationDrawer list instead of blank space)? I run out of ideas.
Finally, I found a solution. My CustomTabAdapter was extension of FragmentPagerAdapter. I changed it to be extension of FragmentStatePageAdapter, and now fragments are recreated.
More details in this answer by #Louth.
It takes fragments from cache without recreation
I'm trying to create an Android application which contains a single activity with a container and a navigation drawer. The initialy empty container loads fragments which has a ViewPager inside a tab layout in which I load a frgment with a FragmentTransaction:
public static void replaceFragmentInContainer(FragmentManager fragmentManager, Fragment fragmentToShow,
boolean addToBackStack)
{
FragmentTransaction transaction = fragmentManager.beginTransaction();
if (addToBackStack)
{
transaction.addToBackStack(null);
}
transaction.replace(R.id.container, fragmentToShow);
transaction.commit();
}
I'm using v4 fragments with a v7 ActionBar in a v7 ActionBarActivity.
Every loaded fragment is a fragment which only loads tabs with other fragments which they hold the actual usability. An example of such tab loading fragment:
public class MainFragment extends TabsFragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
super.onCreateView(inflater, container, savedInstanceState);
View contentView = inflater.inflate(R.layout.fragment_main, container, false);
initTabsLayout(savedInstanceState, contentView, R.id.pager);
addTab("tabspectag1", "", R.drawable.draw1, Fragment1.class, null);
addTab("tabspectag2", "", R.drawable.draw2, Fragment2.class, null);
addTab("tabspectag3", "", R.drawable.draw3, Fragment3.class, null);
return contentView;
}
The problem I'm facing is with the backstack. When a fragment is added to the backstack and I press the back button, the app does go back to the previous fragment like I want to and I see the tabs layout itself, but the content of the tabs is empty like there was nothing loaded to that tab. When it happens, I manage to reload the tab's content only when choosing that screen again with the navigational drawer.
I've tried overriding the onBackPressed in the activity:
#Override
public void onBackPressed()
{
if (getFragmentManager().getBackStackEntryCount() > 0)
{
getFragmentManager().popBackStack();
} else
{
super.onBackPressed();
}
}
but that like I said, it's like I'm getting back to the previous fragment but the tab inside that fragment is not repainting the fragment it had.
What can be done to solve this issue?
Since I DO see the tabs layout of the original fragment in the backstack but not the fragment inside the tab, is it possible I just need to somehow refresh the tab content meaning repaint it? If I can do such a thing then how can I do it?
You didn't post the entire code but, bear in mind that when using fragments inside fragments, the outer fragment should use the childfragmentmanager instead of the regular fragment manager.
If you have an Activity, then you have a fragment that has a Viewpager and inside that viewpager the views are fragments, those outer fragments must use their own childfragmentmanager instead of the activities fragmentmanager.
Activity uses getFragmentManager() to instantiate and show new fragments. Fragments use getChildFragmentManager() to instantiate and show new inner fragments. (fragments inside fragments).
If you always use the same fragmentmanager to handle the transactions the behaviour will be unpredictable.
Your Viewpager should have an TabsAdapter associated that extends from FragmentStatePagerAdapter to show new fragments(and uses the getChildFragmentManager from the fragment instead of the activity).
I created three tabs using FragmentTabHost. Now I need to make all the navigation are under the tabs. How can I get this. I need to get what we get using TabGroupActivity.
Tab1
--->frag1-->frag2
Tab2
--->frag3
Tab3
--->frag4--->frag5
I have used fragmentTransaction.add(), fragmentTransaction.remove(), fragmentTransaction.replace(). These three methods but nothing give solution.
Replace method shows new fragment view on top of existing fragment view.
Remove and Add, from these two remove only works, add does not works.
Thanks in advance.
TabHostMain.java
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_tabs);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
View view;
view=getTabView(R.drawable.ic_launcher);
Bundle b = new Bundle();
b.putString("key", "Simple");
mTabHost.addTab(mTabHost.newTabSpec("simple").setIndicator(view),Fragment1.class, null);
b = new Bundle();
b.putString("key", "Contacts");
view=getTabView(R.drawable.ic_launcher);
mTabHost.addTab(mTabHost.newTabSpec("contacts").setIndicator(view), Fragment2.class, null);
b = new Bundle();
b.putString("key", "Custom");
view=getTabView(R.drawable.ic_launcher);
mTabHost.addTab(mTabHost.newTabSpec("custom").setIndicator(view),Fragment3.class, null);
}
Fragment3.java
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view=LayoutInflater.from(getActivity()).inflate(R.layout.activity_second, null);
((TextView)view.findViewById(R.id.second_act_text)).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fm=getChildFragmentManager();
FragmentTransaction fragmentTransaction=fm.beginTransaction();
fragmentTransaction.replace(R.id.second_activity, new Fragment1()).addToBackStack(null).commit();
}
});
return view;
}
The concept of different fragment navigation stacks in each tab has been discussed quite a few times on stackoverflow, one example for instance:
Separate Back Stack for each tab in Android using Fragments
One simple/crude way of achieving this without having to manage your own custom navigation back stack is to have a root fragment under each tab, and then whenever a root fragment wants to navigate to another fragment (fragment B) simply show a new Activity initially with fragment B and that Activity will have its own fragment navigation back stack.
Tab1 --->root frag1 --> Activity (own nav back stack) --> frag2
Tab2 --->root frag3
Tab3 --->root frag4 --> Activity (own nav back stack) --> frag5 --> frag6 --> frag7
An example of an app that does something like this is actually the StackAnywhere app. It makes heavy use of tabs, but when you navigate within those tabs it generally moves the navigation to a new Activity. YMMV with this approach, however.
I have an app with hierarchy like this:
FragmentTabHost (Main Activity)
- Fragment (tab 1 content - splitter view)
- Fragment (lhs, list)
- Framment (rhs, content view)
- Fragment (tab 2 content)
- Fragment (tab 2 content)
All fragment views are being inflated from resources.
When the app starts everything appears and looks fine. When I switch from the first tab to another tab and back again I get inflate exceptions trying to recreate tab 1's views.
Digging a little deeper, this is what's happening:
On the first load, inflating the splitter view causes its two child fragments to be added to the fragment manager.
On switching away from the first tab, it's view is destroyed but it's child fragments are left in the fragment manager
On switching back to the first tab, the view is re-inflated and since the old child fragments are still in the fragment manager an exception is thrown when the new child fragments are instantiated (by inflation)
I've worked around this by removing the child fragments from the fragment manager (I'm using Mono) and now I can switch tabs without the exception.
public override void OnDestroyView()
{
var ft = FragmentManager.BeginTransaction();
ft.Remove(FragmentManager.FindFragmentById(Resource.Id.ListFragment));
ft.Remove(FragmentManager.FindFragmentById(Resource.Id.ContentFragment));
ft.Commit();
base.OnDestroyView();
}
So I have a few questions:
Is the above the correct way to do this?
If not, how should I be doing it?
Either way, how does saving instance state tie into all of this so that I don't lose view state when switching tabs?
I'm not sure how to do this in Mono, but to add child fragments to another fragment, you can't use the FragmentManager of the Activity. Instead, you have to use the ChildFragmentManager of the hosting Fragment:
http://developer.android.com/reference/android/app/Fragment.html#getChildFragmentManager()
http://developer.android.com/reference/android/support/v4/app/Fragment.html#getChildFragmentManager()
The main FragmentManager of the Activity handles your tabs.
The ChildFragmentManager of tab1 handles the split views.
OK, I finally figured this out:
As suggested above, first I changed the fragment creation to be done programatically and had them added to the child fragment manager, like so:
public override View OnCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstance)
{
var view = inflater.Inflate(Resource.Layout.MyView, viewGroup, false);
// Add fragments to the child fragment manager
// DONT DO THIS, SEE BELOW
var tx = ChildFragmentManager.BeginTransaction();
tx.Add(Resource.Id.lhs_fragment_frame, new LhsFragment());
tx.Add(Resource.Id.rhs_fragment_frame, new RhsFragment());
tx.Commit();
return view;
}
As expected, each time I switch tabs, an extra instance of Lhs/RhsFragment would be created, but I noticed that the old Lhs/RhsFragment's OnCreateView would also get called. So after each tab switch, there would be one more call to OnCreateView. Switch tabs 10 times = 11 calls to OnCreateView. This is obviously wrong.
Looking at the source code for FragmentTabHost, I can see that it simply detaches and re-attaches the tab's content fragment when switching tabs. It seems the parent Fragment's ChildFragmentManager is keeping the child fragments around and automatically recreating their views when the parent fragment is re-attached.
So, I moved the creation of fragments to OnCreate, and only if we're not loading from saved state:
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
if (savedInstanceState == null)
{
var tx = ChildFragmentManager.BeginTransaction();
tx.Add(Resource.Id.lhs_fragment_frame, new LhsFragment());
tx.Add(Resource.Id.rhs_fragment_frame, new RhsFragment());
tx.Commit();
}
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstance)
{
// Don't instatiate child fragments here
return inflater.Inflate(Resource.Layout.MyView, viewGroup, false);
}
This fixed the creation of the additional views and switching tab's basically worked now.
The next question was saving and restoring view state. In the child fragments I need to save and restore the currently selected item. Originally I had something like this (this is the child fragment's OnCreateView)
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)
{
var view = inflater.Inflate(Resource.Layout.CentresList, container, false);
// ... other code ommitted ...
// DONT DO THIS, SEE BELOW
if (savedInstance != null)
{
// Restore selection
_selection = savedInstance.GetString(KEY_SELECTION);
}
else
{
// Select first item
_selection =_items[0];
}
return view;
}
The problem with this is that the tab host doesn't call OnSaveInstanceState when switching tabs. Rather the child fragment is kept alive and it's _selection variable can be just left alone.
So I moved the code to manage selection to OnCreate:
public override void OnCreate(Bundle savedInstance)
{
base.OnCreate(savedInstance);
if (savedInstance != null)
{
// Restore Selection
_selection = savedInstance.GetString(BK_SELECTION);
}
else
{
// Select first item
_selection = _items[0];
}
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance)
{
// Don't restore/init _selection here
return inflater.Inflate(Resource.Layout.CentresList, container, false);
}
Now it all seems to be working perfectly, both when switching tabs and changing orientation.