0

I have 3 fragments.

  • HomeFragment
  • UserFragment
  • ImagesFragment

i use this method for add fragments:

  • add(R.id.frameLayout, HomeFragment, "HomeFragment");
  • add(R.id.frameLayout, UserFragment, "UserFragment");
  • add(R.id.frameLayout, ImagesFragment, "ImagesFragment");

my problem:

i click and start this path:

  1. add HomeFragment
  2. add UserFragment <-- click on follow button (follow btn UI changed Follow Done)
  3. add ImagesFragment
  4. add UserFragment <-- click on follow button (follow btn UI changed UnFollow Done)
  5. press back button back to step 3 -- showImagesFragment`
  6. press back button back to step 2 -- showUserFragment` <-- oops!!! this is my problem . in step 4 i click on follow button and unfollow done. but not change UI in step 2 ? :(

how to update UI on latest change on same fragment?

have you instagram? please check it. open xyz page and start following. after click home btn (do not press back) next open xyz page again and set unfollow. now press back button back to xyz page .you show change follow btn ui

Farzad
  • 1,975
  • 1
  • 25
  • 47
  • plz show your code in `show fragment` and `add fragment` and `update ui` – Yat3s Feb 15 '17 at 08:21
  • @Yat3s code no need. please read my question again – Farzad Feb 15 '17 at 08:25
  • The problem is `UserFragment` is a single instance or two instance? – Yat3s Feb 15 '17 at 08:33
  • @Yat3s i think two instance. have you instagram? please check it. open xyz page and start following. after click home btn (do not press back) next open xyz page again and set unfollow. now press back button back to xyz page .you show change follow btn ui – Farzad Feb 15 '17 at 08:41
  • 1
    I got your problem, there are have 2 instance, you should notify other Fragment to update UI, you can use `EventBus` or `RxBus`. – Yat3s Feb 15 '17 at 08:48
  • @Yat3s Unfortunately I do not know the meaning of `instance`. i use add `UserFragment` for 2 times or more ... instagram used by `EventBus` or `RxBus` or similar lib? – Farzad Feb 15 '17 at 08:55
  • Sorry my English is not very good, `instance` means step2 and step4 is not use same `UserFragment` , so step2 `UserFragment` follow button is Followed state, but step4 `UserFragment` follow button is unFollowed state. – Yat3s Feb 15 '17 at 09:21
  • @Yat3s thank you for your idea – Farzad Feb 15 '17 at 09:34

1 Answers1

1

In your UserFragment, add onHiddenChanged callback to refresh your UI. If refresh is not always needed on resume back of UserFragment, use shouldReloadScreen(add it in your activity) flag like below:

@Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            unregisterEventBus();
        } else {
            registerEventBus();
            if (((YourActivity) context).shouldReloadScreen()) {
                loadData();
                ((RevolveActivity) context).setshouldReloadScreen(false);
            }   
        }
    }

If it not necessary for you to show ImageFragment while moving back from step 4, use below approach:

While moving from step 3 to step 4, instead of adding UserFragment remove ImageFragment. This will keep only one instance of UserFragment in the stackTrace.

nnn
  • 980
  • 6
  • 13
  • my 6 steps is example. in instagram or spotify this update ui worked good. instagram allow t o user open unlimit pages and posts. and with press back button navigation is nice work. in instagram if you open 10 times same page, older pages update ui on change on new pages – Farzad Feb 15 '17 at 09:40