I have this scenario implemented where I have a navigation drawer with nested fragments that are added to the back stack with tag. Every fragment has the following implemented:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (rootView == null) {
rootView = inflater.inflate(R.layout.fragment_feed, container,
false);
// Doing initialisaion here
} else {
((ViewGroup) rootView.getParent()).removeView(rootView);
}
return rootView;
This was the best way I could think to maintain my lists inside these fragments and their dynamic headers and footers (Thinking about implementing a new approach here already).
Furthermore I have these broadcasts when a user loges out of my app so that I refresh these lists, or when the location changes:
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
locationBroadcastReciever,
new IntentFilter(UpdateLocationIntentService.LOCATION_CHANGED));
LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
significantBroadcastReciever,
new IntentFilter(UserDefaultsManager.SIGNIFICANT_CHANGED));
// Further code here
}
The on detach is called when the fragment is detached:
@Override
public void onDetach() {
super.onDetach();
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(
locationBroadcastReciever);
LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(
significantBroadcastReciever);
// This is also where I cancel any pending request for fetching data
};
This way is right now working but I have to check when a request is finished whether some of the views have been destroyed because onDestroyView is called when I replace the fragment with another but onDetach is not called since the fragment is on back stack and its not detached yet.
The current implementation with view nullpointer checks is allowing me to update 4 fragments simultaneously when a user is logged out (or logged in) or location changed and there were no items previously on the list.
I am sure that there is a better way, but I cannot seem to get the same results at what ever else I try.