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();
Related
I want refresh my fragment when I click on button which inside in this same fragment. I tried some code but when I click on button my app automatically crash.
I try these codes π
Fragment frg =null;
frg=getActivity().getSupportFragmentManager().findFragmentByTag(getFragmentTag(4));
final FragmentTransaction ft =
getActivity().getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
And I try this for getting the tags of fragment because I'm using ViewPager.
And I'm Using Androidx Libraries.
My code is this for getting tag. π
public String getFragmentTag(int pos){
return "android:switcher:"+R.id.view_pager_main+":"+pos;
}
I solved my problem just using thisπ
getActivity().getSupportFragmentManager().beginTransaction().detach(FragmenViews.this).attach(FragmentViews.this).commit();
Im currently working on a sample app that will be using fragments, but im having a little problem regarding changing the fragment.
My default layout for my fragment is fragmentOne (see code below).
<fragment
android:id="#+id/fplace"
android:layout_width="match_parent"
android:layout_height="498dp"
android:name="layout.FragmentOne"
tools:layout="#layout/fragment_fragment_one" />
and that's working correctly.
but when im changing the fragment it appends. the behavior must replace the fragment not append.
here is my code for the activity (see code below).
public void onClick(View view) {
Button button = (Button) view;
Fragment fragment = null;
switch (button.getId()) {
case R.id.f1:
fragment = new FragmentOne();
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fplace, fragment);
ft.commit();
break;
case R.id.f2:
fragment = new FragmentTwo();
fm = getFragmentManager();
ft = fm.beginTransaction();
ft.replace(R.id.fplace, fragment);
ft.commit();
break;
}
}
and from the fragment one i am loading the data from the onCreateView() which is working correctly. the main problem is the fragment appends not replace.
(see photo below)
the red and yellow must be replaced by the blue colored fragment
As Mike said in comment
You cannot remove/replace Fragments that are declared in your layout.
So here is solution for you. Instead of fragment in xml use framelayout as container which will hold the fragment
use this instead of fragment in xml
<FrameLayout
android:id="#+id/fplace"
android:layout_width="match_parent"
android:layout_height="498dp"
/>
Hope this will help you.
declare a new fragmenttransaction in the second case dont use the fragment transaction that is used in the first case
I have an activity in which layout has Tabs using TabLayout and viewpager from fragments. There are two tabs and having 2 fragments 1 for each tab.
Now I am on second Tab's Fragment, when I perform any action in this fragment, I have to reload that fragment, but when I am restarting that activity using the below code:
((Activity)mContext).finish();
mContext.startActivity(((Activity) mContext).getIntent());
It reloads the first fragment, I want to reload the second fragment, how can I achieve this.
Thanks you so much for your help.
Try to refresh your current Fragment:
// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.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();
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