I have an Activity
with a fragment container in layout.
3 different Fragment
can be displayed in it.
These fragments contains a Listview
which displays data with custom Adapter i made.
So each list elements is created during onCreateView
, after i queried a database to have data.
But sometimes some data may changes in my database, so i would like to redraw/recreate it the Listview
.
Go to your navigation graph and find the fragment you want to refresh. Create an action from that fragment to itself. Now you can call that action inside that fragment like so.
Back button should be handled from Activity. You can override onBackPressed in Activity and call a function on corresponding fragment to reloadItems().
The onResume() get called always before the fragment is displayed. Even when the fragment is created for the first time . So you can simply move your from onViewCreated() to onResume . Save this answer.
addOnPageChangeListener(new ViewPager. OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { // do this instead, assuming your adapter reference // is named mAdapter: Fragment frag = mAdapter.
Combined two answers and removed if (isVisibleToUser)
, because it makes the setUserVisibleHint
be called in an unpredicted asynchroneous order and fragment can either be refreshed or not. I found this piece of code stable (in your Fragment):
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
// Refresh tab data:
if (getFragmentManager() != null) {
getFragmentManager()
.beginTransaction()
.detach(this)
.attach(this)
.commit();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With