I have a fragment and I need to completely refresh/reinstantiate it.
By refreshing I mean recreating fragment.
I have tried using FragmentManager with detach() method but it didn't help. All EditText children of that fragment still have their values entered even though the fragment was refreshed
Is there any way to achieve this result?
if there are only edit texts , create a method that sets all edit texts to empty string that is et(edit_text object),
et.setText(""); //edit text will be reset
Put the following code inside the fragment which need to be refreshed.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
Use fragment manger according to your code (if it is an old version then this will be supportFragmentManager)
Put and call this method to your fragment
private void reloadFragment(){
Fragment frg = getActivity().getSupportFragmentManager().findFragmentByTag(ReceiptFragment.TAG);
final FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();
}
Related
I have a Recycler View in a Fragment that I have an adapter class for in a separate file. I am trying to make it so that when you click on a RecyclerView Item it replaces the current fragment with a new one. The code to replace a fragment with another that I had been using previously is this:
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.nav_host_fragment, NewFragment.class, null);
transaction.addToBackStack("CurrentFragment");
transaction.commit();
And this works inside the fragment, but since I have to call it from the java class I tried this instead:
FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.nav_host_fragment, NewFragment.class, null);
transaction.addToBackStack("CurrentFragment");
transaction.commit();
And this just layers the new fragment on top of the current one. If I try making a class in the Current Fragment with the transaction code in it and call it, it doesn't work because of the static context.
Is there any way to do this?
I have a view pager that returns same fragment with different data from adapter every time i swap but despite of the fact that different data is received it displaying the same data over and over again, how can i make fragment update its data
the structure is, there is a fragment that has a listview and when i click on the listview item it replaces a fragment that has viewpager and setting its adapter, the adapter is FragmentPagerAdapter and returns a fragment with argument now fragment uses its argument and display data, every time getview called of adapter returning the same fragment with different data
here is the code:
when list item clicked:
replaceFragment(ReportPaginationFragment.newInstance(i, isFromList), true, true);
ReportPaginationFragment:
viewPager.setAdapter(new ReportPaginationAdapter(getFragmentManager(), reportID, appointmentObjectArrayList, isFromList, listPosition ));
Adapter:
reportId = appointmentObjectArrayList.get(item++).getId();
return NewDrReportFragment.newInstance(reportId);
appointmentObjectArrayList is arraylist that has data
Thanks in advance
Instead of addFragment, replace is better choice.
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
for (int i=0;i<categoryList.size();i++){
Category category=categoryList.get(i);
FoodFragment foodFragment = new FoodFragment();
foodFragment.setCategory(category);
pagerAdapter.addFragment(foodFragment, category.getName());
}
lets take this example. Maybe you are not setting differrent data onto pageAdapter properly.
can you share your code for more information?
i have below code for data remove from DB.
So i delete data but i want to update fragment from here.
how i can refesh fragment ?
// Remove entry from database table
database.delete(FAVORITES_TABLE, FAVORITES_ITEM_ID + " = " + soundObject.getItemID(), null);
// Refesh current fragment
????????????????????????????????
this is code which i use in Fragment to refesh it (on button press) work good, but i don't want to tap refesh button to refesh page. I want fragment refesh when DB change by it self.
public void refersh()
{
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
}
any idea?
Thanks
you are detaching and attaching again in one commit, so it does nothing. make it two commits
public void refersh(){
FragmentTransaction ft1 = getFragmentManager().beginTransaction();
ft1.detach(this).commit();
FragmentTransaction ft2 = getFragmentManager().beginTransaction();
ft2.attach(this).commit();
}
there is also possibility that you shoud use getSupportFragmentManager instead of getFragmentManager
its probably not best/most-efficient solution, but you posted ony this small snippet...
In simple terms you can use:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
I think you want to refresh the fragment contents upon DB data updates
If so, detach the fragment and again attach it
// Reload current fragment
Fragment f1 = null;
f1 = getSupportFragmentManager().findFragmentByTag("Fragment_TAG");
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.detach(f1);
ft.attach(frg);
ft.commit();
Fragment_TAG is the name you gave your fragment when you created it
To refresh the current fragment content, you can use the following code inside the method refresh()
public void refresh()
{
Fragment fragment = null;
fragment = new Current_Fragment_Name(); //replace the Current_Fragment_Name with your current fragment name
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
Hope it works for you.
I have a few fragments in one activity. In Main Activity I implemented popBackStack(); in onBackPressed();
So that, you can always go back to previous fragment clicking back button. But popBackStack(); doesn't remove the fragment I just left.
How to achieve removing current fragment each time, we click back
button?
public void removeFragmentbyTag(String myFrag){
FragmentManager manager = getActivity().getSupportFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove(myFrag);
trans.commit();
manager.popBackStack();
}
hope this bit of code help you.
I've been working on a project that has swipeable views with tabs.
The tabs exist as fragments. I want to run a fragment method from the main activity on a periodic basis.
To run the methos, I dont' have the ID for the fragment. I read in other posts that:
String tag = "android:switcher:" + TabActivity.this.viewPager.getId() + ":" + "0";
Tab1Fragment fragment = (Tab1Fragment) getSupportFragmentManager().findFragmentByTag(tag);
fragment.someMethod();
will return the fragment I desire, which happens to be the zero-th or first tab. However, running this code returns a nullpointer exception.
Any help will be appreciated. Thanks :)
In your code, at some point you must have:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.framelayout, new SomeXYZFragment(), "some_xyz_fragment");
ft.commit();
Add the line:
fm.executePendingTransactions();
after the line ft.commit();
Try this. It should work.