I want to replace a Fragment from a defined class FluxListeFragment with another class FluxDetailFragment (and use addToBackStack fonctionnality of the FragmentTransaction). Thus, I add my fragments dynamically on the onCreate of my activity :
FluxListeFragment FragmentListe = new FluxListeFragment();
fragmentTransaction.add(R.id.FL_Portrait, FragmentListe, "fragment_portrait");
fragmentTransaction.commit();
Then, when I want to replace FragmentListe with FragmentDetail, I code this :
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
FluxDetailFragment FragmentDetail = new FluxDetailFragment();
fragmentTransaction.replace(R.id.FL_Portrait, FragmentDetail, "fragment_portrait");
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
But I get an InvalidClassCastException, since I want to replace a Fragment from a class with a fragment from another class.
How to make this work ?
Related
I have implemented fragments in my application. Here my code for swiching fragment in fragment_container.
private void switchFragment(Fragment fragment, boolean isAddToBackStack, String tag)
{
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.container, fragment, tag);
if (isAddToBackStack)
ft.addToBackStack(tag);
setCurrentTopFragment(Integer.parseInt(tag));
ft.commit();
}
I have 4 fragments A,B,C and D and for switching between this fragmnets I am using above method. I have A,C,B in my backstack. If again I switch to fragmnet A, my backstack is like A,C,B,A. What I actually want is If I swich to A again I want backstack sequence like this C,B,A. Means Remove old instance from backstack and add new to it.
First get the back-stacked Fragment by id which you need to remove:
Fragment fragment = getSupportFragmentManager().getFragment(new Bundle(), TAG_KEY)
or there are several methods getBackStackEntryCount(), getBackStackEntryAt. After getting the fragment which you need to remove. Remove it from the fragment back-stack.
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove(fragment);
Then you can add a new fragment Done :)
I'm trying below code to replace the new fragment, but it's not
Replaced.
android.app.Fragment Manager fragmentManager = ((Activity) context).
getFragmentManager(); android.app.FragmentTransaction
fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame_container,
menuItemDetailsFragment); fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
You actually need to use the replace function
Just replace this line of code
fragmentTransaction.add(R.id.frame_container, menuItemDetailsFragment);
with this
fragmentTransaction.replace(R.id.frame_container, menuItemDetailsFragment);
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 use the one frame on which changing the fragment views, the problem I have is when I place add instead of replace the new view is placed on the existing one
all I need is it has to replace the existing and place the new one
As I have to reuse the current view when back button is pressed cannot use the replace
Fragment fragment = null;
fragment = new DetailFragment2();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.frame_container2, fragment)
.addToBackStack(null).commit();
If you don't use the support library (which is your actual case), use this code:
final FragmentTransaction ft =
getFragmentManager().beginTransaction();
ft.replace(R.id.frame_container2, new DetailFragment2());
ft.commit();
If one day you'll decide to use the support library, then use this instead (it's only a matter of replacing getFragmentManager() with getSupportFragmentManager()):
final FragmentTransaction ft =
getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frame_container2, new DetailFragment2());
ft.commit();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction;
transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.frame_container2,fragment.newIstance());
transaction.addToBackStack(null);
transaction.commit();
And you need to create newIstance in Fragment class:
public static fragment newIstance(){
fragment frag= new fragment();
return frag;
}
I have a Fragment called MyFrag and I create two instance of this Fragment, frag1 and frag2. In frag1 I register a click listener, and in the callback of this click listener I want to perform a FragmentTransaction and replace frag1 with frag2.
I managed to replace Fragments in an Activity but Fragments don't have the getSupportFragmentManager() method. This is the code I am using:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_container, new Flashcard_Fragment(false, 3));
transaction.commit();
Use getFragmentManager() in the Fragment instead of getSupportFragmentManager(). getSupportFragmentManager() is only used in the FragmentActivity. Try this:
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.fragment_container, new Flashcard_Fragment(false,3));
transaction.commit();
Also be sure that Flashcard_Fragment has an empty constructor as well! The OS requires Fragments to have an empty public constructor.
You can just call getFragmentManager() in your Fragment.
The FragmentManager you get from getFragmentManager() is used to replace/add Fragments to the Activity.
The FragmentManager you get from getChildFragmentManager() is used to replace/add Fragments inside of other Fragments.
So it seems that you just need to use getFragmentManager().