1. Are you ready for the Galaxy S20? Here is everything we know so far!

need to replace setUserVisibleHint in AndroidX as it is deprecated, can anybody help

Discussion in 'Android Development' started by Nirmala srivastava, Apr 18, 2020.

  1. Nirmala srivastava

    Thread Starter

    I have 3 fragments attached with viewpager.
    I have a single database and all three fragments data is connected to each other so if i make any change in fragment 1 & it should also be reflected in fragment 2(when it is visible to user)
    previously i was using
    Code (Text):
    1.  @Override
    2.     public void setUserVisibleHint(boolean isVisibleToUser) {
    3.         super.setUserVisibleHint(isVisibleToUser);
    4.         if(isVisibleToUser){
    5.             getFragmentManager().beginTransaction().detach(this).attach(this).commit();
    6.         }
    7.     }
    but setUserVisibleHint is deprecated so i need to replace it but cloudn't find proper solution.
    I tried to used "detach().attach()" in onResume but it goes to infinite loop then.

    Note: i tried to use "notifyDataSetChanged()" instead of detach.attach but it doesn't work properly and takes time to reflect data.
    I tried to recreate activity but it push the recyclerView on first item. I want my recyclerView to be show on the same state where it previously was like if it was on item no 24 before leaving it then whenever user comeback on same fragment by doing detach.attach data is refreshed and recyclerView is on same position.

    Please tell me the alternative solution for detach.attach on every desired fragment is visible to user.


    1. Download the Forums for Android™ app!



Share This Page