UPDATE: It looks like you're trying to tell an Activity that it's time to change the content fragment depending upon a selection in a Navigation Drawer.
This problem has two parts.
- You need to provide a mechanism to communicate from your Drawer to your Activity, which, will, in turn, proceed to perform the necessary actions.
- Your Fragments must either be Detached, Destroyed, Recreated and Reattached (inneficient, why?) or simply implement a mechanism that tells them: Hey, it's time to Refresh your content.
I don't see any reason why you need to completely destroy the Fragment's view if you only need to reset its data. Seems like a design flaw.
Replacing a Fragment should not mean you want to completely destroy it, since the user can simply go back or press the recently removed fragment. It would be inefficient to recreate it again.
Like I mentioned in the comments, more code is needed to see what your current approach is.
Normally a simple Interface (like the one I described below) should suffice. Your activity can receive the "clicks" in the Drawer and decide which fragment to replace (if needed).
Please be a lot more specific and provide more code.
Old Response:
Warning: if you're trying to replace the current fragment, make sure you're not getting the wrong FragmentManager. You do getFragmentManager()
but I'm sure you're using the support library and therefore need to do getSupportFragmentManager();
.
You've failed to mention what version you're targeting so it's hard to know.
On the other hand,
If what you want to do is tell the current visible fragment to refresh… you should use a more common approach to object-to-object communication.
You're over-complicating things.
(warning: pseudo code)
Create an interface like:
public interface Refreshable{
void onShouldRefresh();
}
Have a DrawerController capable of storing observers/listeners…
public class DrawerController {
private List<Refreshable> mListeners = new ArrayList<Refreshable>();
public void addRefreshListener(E listener) {
if (listener != null && !mListeners.contains(listener)) {
mListeners.add(listener);
}
}
public void removeRefreshListener(E listener) {
if (listener != null) {
mListeners.remove(listener);
}
}
}
Make your fragments implement it (and subscribe to the event when they are visible)
public class HomeFragment extends Fragment implements Refreshable {
// your fragment code
public void onShouldRefresh(){
// do the refresh
}
@Override
public void onResume() {
super.onResume();
yourDrawerController.addRefreshListener(this);
}
@Override
public void onPause() {
super.onPause();
yourDrawerController.removeRefreshListener(this);
}
}
Now make a method that will tell the interested parties (in this case your "Refreshable" objects) that it's time to refresh, add this to your DrawerController…
public void refresh(){
for (Refreshable listener : mListeners) {
if (listener != null) {
listener.onShouldRefresh();
}
}
}
Finally, in your code, call refresh()
and have a beer.
Did I miss your point? it's not 100% clear to me.