0

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.

Agon Lohaj
  • 33
  • 1
  • 4
  • Looks like you could benefit from taking a look at EventBus – cYrixmorten Oct 28 '14 at 19:17
  • Or you can try Otto that alternative to EventBus. – Emre Aktürk Oct 30 '14 at 13:06
  • 1
    Thank you for the suggestions. I don't think we are at the right track. The events are propagated through my fragments (this is not the problem). I did a better workaround by setting up a flag so that the fragment needs to be updated and whenever onResume is called I would do this refresh (this way I don't have to worry that my views are null). – Agon Lohaj Oct 30 '14 at 23:34
  • It solved my issue, but it is not updating if the activity is in active state, i mean not paused. – Nicks Aug 14 '15 at 02:25

1 Answers1

0

My case was to update the textfield in one of the fragments hosted by MainActivity.The simplest solution I found was this:--

In my MainActivity class retrieved the running instance of MainActivtiy.This is my MAinActivity

private static MainActivity mainActivityRunningInstance;
    public static MainActivity  getInstace(){
        return mainActivityRunningInstance;
    }
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mainActivityRunningInstance =this;
----------
}

Now in Broadcast reciever's onRecieve method, get that instance and call the update method

 @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().matches(Intents.PROVIDER_CHANGED_ACTION)) {
                String textValue="the text field value";
// the null check is for, if the activity is not running or in paused state, in my case the update was required onlyif the main activity is active or paused
            if(MainActivity.getInstace()!=null)
                MainActivity.getInstace().updateUI(textValue);
}

Now for the part where we need to run the update in UIThread,The updateUI method of MainActivity will call the fragments update method.

public void updateUI(final String str) {
        MainActivity.this.runOnUiThread(new Runnable() {
            public void run() {
     //use findFragmentById for fragments defined in XML ((SimpleFragment)getSupportFragmentManager().findFragmentByTag(fragmentTag)).updateUI(str);
            }
        });
    }

and the final step is updating the fragment's text field

public void updateUI(String str){
        tv_content.setText(str);
    }

and Bingo, its done. I referred post to solve my issue. Hope it help others.

Community
  • 1
  • 1
Nicks
  • 16,030
  • 8
  • 58
  • 65