I have a litle doubt.
I have an activity that have 3 fragment inside. I need to restart the state of one of these fragments. Restart only one.
Old, but may be useful to someone else. To refresh the fragment, you need to detach the fragment and reattach it
Fragment frg = null;
frg = getFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
Your_Fragment_TAG is the name you gave your fragment when you created it
Related
everyone, my issue is :
I'm in ProfilFragment, that was generated with the Menu Drawer it extends Fragment, and I can't change this (it broke some other part).
When I click on a button I want to refresh my current Fragment
// Reload current fragment
Fragment frg = null;
frg = getActivity().getSupportFragmentManager().findFragmentById(R.id.nav_profil);
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
I found this on other StackOverflow issues but it didn't work for me.
frg = getActivity().getSupportFragmentManager().findFragmentById(R.id.nav_profil);
Always return null
Even if in my mobile_navigation.xml
I have
<fragment
android:id="#+id/nav_profil"
android:name="com.example.pierregignoux.ui.profil.ProfilFragment"
tools:layout="#layout/fragment_profil" />
Any help would be great, Thanks a lot.
just run this
getActivity().getSupportFragmentManager().beginTransaction().detach(this).attach(this).commit();
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 have 1 activity with multiple fragments. These fragments call each other, for example fragment a calls fragment b:
Fragment a:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, new FragmentB());
ft.commit();
Where OrderAddFragment is fragment b.
Now, let's say a user has filled in multiple inputs in fragment a and continues to fragment b but than he realises he made a mistake in fragment a and needs to go back to correct this mistake.
How could I reopen fragment a with the saved instance state instead of creating a new instance?
The back function in fragment b uses following code to reopen fragment a:
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_container, new FragmentA());
ft.commit();
FragmentTransaction.addToBackStack() might solve your problem.
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 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();