I am navigating from one fragment to another fragment but my title alone changes my screen remains same. In the background the new fragment is up and running. This happens only when i navigate during any api call in the background.
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
if (main_fragment_container != null) {
ft.replace(main_fragment_container.getId(), fragment, fragment.getClass().getName());
}
if (isStackNeeded) {
ft.addToBackStack(fragment.getClass().getName());
}
ft.commit();
Related
I have this problem in an app, where that method below doesn't always effectively hide the currently visible fragment (contentFragment) and shows the new one (fragment). Basically, sometimes, it does the complete opposite of what it's supposed to do, that is, the current fragment remaining visible, and the new one not showing at all. And what is worth, is that I can't always replicate when it happens.
I'm really at wits' end here. Anyone got any ideas?
public void addContentFragment(GenericFragment fragment) {
FragmentManager fragmentManager = slidingMenusActivity.getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.ContentLayout, fragment, fragment.getFragmentTag());
transaction.addToBackStack(fragment.getFragmentTag());
Log.d(TAG, "addContentFragment hiding contentFragment=" + contentFragment);
transaction.hide(contentFragment);
transaction.commit();
fragmentManager.executePendingTransactions();
contentFragment = fragment;
}
Your contentFragment sometimes do not have the value you are expecting, try to retrieve the current fragment to ensure you are really hiding the fragment before your new transaction.
Modify your addContentFragment(GenericFragment fragment) as the follows:
public void addContentFragment(GenericFragment fragment) {
FragmentManager fragmentManager = slidingMenusActivity.getSupportFragmentManager();
Fragment currentFragment = fragmentManager.findFragmentById(R.id.ContentLayout);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.ContentLayout, fragment, fragment.getFragmentTag());
transaction.addToBackStack(fragment.getFragmentTag());
Log.d(TAG, "addContentFragment hiding contentFragment=" + contentFragment);
transaction.hide(currentFragment);
transaction.commit();
}
I have 2 fragment on one fragment activity. I have 2 fragments say new and pending but I am having problem while switching from one fragment to another while tapping of button. When I am on pending tab and then click on new tab then sometimes it shows two loader and then it still shows pending page. My both fragments are android.support.v4.app.Fragment. I am attaching screenshot of the problem while I am tapping on new tab and still showing pending tab with two loader.
code for switching between fragment
#Override
public void onClick(View v) {
if (v.equals(neww))
{
Fragment newpage = new NewPageActivity();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.framelay, newpage);
ft.commit();
}
else if(v.equals(pending))
{
Fragment pendingFragment = new PendingPage();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.framelay, pendingFragment);
ft.commit();
}
}
now screenshot of problem
Try this:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.framelay, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
I'm trying to implement the first Activity of an app with the BottomNavigation. This will have three tabs whose will open three different fragments.
The landing fragment is a MapView then you can access to the other fragments so for it I replace them.
private void replaceFragment(Fragment fragment, String tag) {
FragmentManager fragmentManager = getFragmentManager();
if(fragmentManager.findFragmentByTag(tag) != null) {
fragmentManager.popBackStack(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}else {
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.framelayout_home_content, fragment, tag);
ft.addToBackStack(tag);
ft.commit();
}
}
Every time I replace my fragment and go back to the Map the map is built again. Is there any way to keep the Map or speed up the process in order to avoid flickr every time it is shown?
should I addToBackStack and then Pop It up?
Indeed I already tried it but I don't think I've done it right
private void showSearchMap() {
if (getFragmentManager().getBackStackEntryCount() == 0) return;
FragmentManager fm = getFragmentManager();
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
in order to avoid open a fragment once it is already visible:
if( getFragmentManager()
.findFragmentById(R.id.framelayout_home_content)
instanceof SettingsFragment ) return;
Is there any proper way to handle this BottomNavigation with Fragments?
I have added 3 fragments to my Activity with
String name = "fragment1"; // and ..2 and ..3
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.content_frame, fragment, name);
fragmentTransaction.addToBackStack(name);
fragmentTransaction.commit();
The last added (third) Fragment now is visible on top.
Now I want to resume to the first added Fragment. But how?
I can find this Fragment with
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment firstFragment = fragmentManager.findFragmentByTag("fragment1");
If I call fragmentManager.getFragments() I still can find all three Fragments.
How to bring firstFragment back to top, make it visible again?
You can hide your 2nd and 3rd fragment and make your 1st fragment visible. So you'll have the effect that first fragment is shown on top and others are invisible.
solution:
Use the FragmentTransaction's show and hide method. Firs you need to find all the fragment and call the FragmentTransaction to show and hide 2nd and 3rd fragments.
Here's how I do it in my app when I want to switch to fragment:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
if (fragment.isAdded())
{
transaction.show(fragment);
}
else
{
transaction.replace(R.id.container, fragment);
}
transaction.addToBackStack(null);
transaction.commit();
Note the use of show.
It ended up with this:
/**
* #param tag name of the fragment to resume and to bring to top
* #return true if fragment has been found
*/
private boolean bringFragmentToTop(String tag) {
FragmentManager fragmentManager = getSupportFragmentManager();
Fragment fragment = fragmentManager.findFragmentByTag(tag);
if (fragment != null) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
for (Fragment f : fragmentManager.getFragments()) {
if (f == fragment)
fragmentTransaction.show(f);
else
fragmentTransaction.hide(f);
}
fragmentTransaction.commit();
return true;
}
return false;
}
After struggling to make my simple fragment program work, I have not found any solution to the following: I manage two Fragments in my main activity: FragmentNeedle and FragmentPlot. Only one should appear at a time. The user has two bottons where he can select which fragment he wants to display. Also, when the FragmentPlot is showing, the user should be able to navigate back to the FragmentNeedle by pressing the back key. This is my Code:
public void onButtonPlotPressed()
{
FragmentManager manager = getSupportFragmentManager();
mPlotFragment = (PlotFragment) manager.findFragmentByTag(PlotFragment.class.getSimpleName());
if(mPlotFragment == null)
{
mPlotFragment = new PlotFragment();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(R.id.main_layout_center, mPlotFragment, PlotFragment.class.getSimpleName());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
manager.executePendingTransactions();
}
public void OnButtonNeedlePressed()
{
FragmentManager manager = getSupportFragmentManager();
mFragmentNeedle = (FragmentNeedle) manager.findFragmentByTag(FragmentNeedle.class.getSimpleName());
FragmentTransaction ft = manager.beginTransaction();
if(mFragmentNeedle == null)
{
mFragmentNeedle= new FragmentNeedle();
ft.replace(R.id.main_layout_center, mFragmentNeedle, FragmentNeedle.class.getSimpleName());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
manager.executePendingTransactions();
}
}
When the PlotFragment is shown, and I press back, I return to the NeedleFragment. But now when I try to change to Plot Fragment by pressing the UI button, it will keep showing the PlotFragment. If I remove the line ft.addToBackStack(), the switching between fragments works fine by pressing the buttons on the UI, but then I cannot go back with the back key. What am I doing something wrong?
you never add your FragmentNeedle to the backstack.
public void OnButtonNeedlePressed() {
mFragmentNeedle = (FragmentNeedle)manager.findFragmentByTag(FragmentNeedle.class.getSimpleName());
if(mFragmentNeedle == null) {
FragmentManager manager = getSupportFragmentManager();
mFragmentNeedle= new FragmentNeedle();
ft.replace(R.id.main_layout_center, mFragmentNeedle, FragmentNeedle.class.getSimpleName());
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//missing line
ft.addToBackStack(FragmentNeedle.class.getSimpleName());
ft.commit();
manager.executePendingTransactions();
}
}