In my application there is a FrameLayout on which I am adding various fragments
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_content, fragment, fargmentTag);
ft.commit();
Now it seems when I use the ft.replace(R.id.fragment_content, fragment, fargmentTag); and then in other places call
getSupportFragmentManager().findFragmentByTag(fargmentTag);
I always get null.
However if I use add instead of replace this problem is fixed but another problem appears that each fragment is added ontop of the other fragment and i can see the other fragments below.
I would prefer to use replace, but I need the saved fragment state. Also to be noted I am not using addToBackStack.
Try this and it would work.
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_content, fragment, fragmentTag);
ft.commitAllowingStateLoss();
You just basically answered your own question. Use addToBackStack().
FragmentManager: fm = getSupportFragmentManager(); fm.executePendingxxxx,try it.
Related
I have a fragment in which there is a nested fragment which I add in this way:
if (home == null) {
home = new MyFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
transaction.addToBackStack(MyFragment.class.getName());
transaction.add(R.id.child_fragment, home).commit();
}
When I enter another fragment and go back the child fragment from above is not there. I checked and the instance is different from null.
UPDATE: I changed the code as suggested by Ashwin S Ashok but it's still not working.
Try using these methods:
// Use this if you don't want to retain the fragment.
protected void replaceFragmentStack(int container, Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(container, fragment);
fragmentTransaction.commit();
}
// Use this if you want to add the fragments in a stack.
protected void addFragmentStack(int container, Fragment fragment, String tag) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
fragmentTransaction.add(container, fragment, tag);
fragmentTransaction.addToBackStack(tag);
fragmentTransaction.commit();
}
I would suggest you to use getChildFragmentManager() when making transactions inside a fragment. And its a bug i guess.
You can check out this thread it will help you alot Android 4.2: back stack behaviour with nested fragments
Also you need to go through The Curious Techizen's blog
Here is the link for the github project sample for same mechanism
I hope this will help you.
How to change the whole view of a fragment with other fragment !!
Or how to close the current fragment with another fragment, please explain with layout also
Thanks in advance...
You can either add or replace fragments in your activity. Create a FrameLayout in activity's layout xml file.
Then do this in your activity to replace fragment. You can use same code each time you want to replace one fragment with other.
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.container,YOUR_FRAGMENT_NAME,YOUR_FRAGMENT_STRING_TAG);
transaction.addToBackStack(null);
transaction.commit();
If you want to add fragment instead of replace then do this:
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.container,YOUR_FRAGMENT_NAME,YOUR_FRAGMENT_STRING_TAG);
transaction.addToBackStack(null);
transaction.commit();
When you want to replace added frogment with anu other fragment then you have to remove previous fragment first (or you can hide previous fragment; depends on your requirement). See following code:
Fragment fragment = getSupportFragmentManager().findFragmentByTag(YOUR_FRAGMENT_STRING_TAG);
if(fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
See following related questions on SO:
Difference between add(), replace(), and addToBackStack()
Basic difference between add() and replace() method of Fragment
Difference between add() & replace() with Fragment's lifecycle
Or see my answer to a similar question:
How to start Fragment from an Activity
getSupportFragmentManager().beginTransaction().replace(R.id.fragmentlayout,new fragment()).commit()
This will help you replace your existing fragment in view with id FragmentLayout with a new fragment().
ThankYou i hope this was helpful.
First you take one Framelayout in Your Activity where you add fragment.
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.framelayout,fragment_1);
transaction.addToBackStack(null);
transaction.commit();
When you replace first fragment with second one you write, just change fragment_1 to fragment_2
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.framelayout,fragment_2);
transaction.addToBackStack(null);
transaction.commit();
I am new to android. I am trying to develop an app using android fragments.
Initially it works nicely.Once i close and reopen the app means fragment not loaded. If anyone knows please let me.
i tried following code
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
MapFragment llf = new MapFragment();
ft.replace(R.id.id_new_map, llf);
ft.commit();
but not working.
As Mimmo is directing ... you probably will want to move the code to onResume().
Also, you should use the Fragment Manager, something like below.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.list_alphabet_layout, new MapFragment(), getResources(R.id.id_new_map));
fragmentTransaction.commit();
boolean wereThereWereAnyPendingTransactions = fragmentManager.executePendingTransactions();
I want to open a fragment, which does not lie in the list of tabs, from a fragment in an onClick() function.
I have been struggling with this for a while now. Can someone give some pointers?
All the solutions that I have seen transition from one fragment to another(both being part of the tab list).
Thanks.
Create a instance of the fragment and use fragment Manager
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
LinkListFragment llf = new LinkListFragment();
ft.replace(R.id.container, llf);
ft.commit();
So, first of all I create a new Fragment like this
ft = fm.beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.replace(R.id.main_content_frame, cFr, "CARS");
ft.addToBackStack(null);
ft.commit();
And later I remove it like this
fm.popBackStack();
ft = fm.beginTransaction();
ft.setTransition(FragmentTransaction.TRANSIT_NONE);
ft.remove(fm.findFragmentByTag("CARS")).commit();
But the close transition is done with the TRANSIT_FRAGMENT_OPEN animation (or its opposite by default, I think), and I clearly set TRANSIT_NONE.
Any thoughts?
public void mRemoveFragment(android.app.Fragment fragment){
android.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
ft.remove(fragment);
ft.commit();
}
use this function to remove the fragment. Where in setCustomAnimation, you can give your scripts. I have currently used the default ones provided by android
Try using a custom animation with the FragmentTransaction:
fragmentTransaction.setCustomAnimations(R.anim.frag_fade_in, R.anim.frag_fade_out, R.anim.frag_fade_in, R.anim.frag_fade_out);
Resources here :: Custom animations for fragment transactions