Improve performance between changing tabs using fragments and recyclerview an android studio

1

I am making a small application to take control of a sticker album when I change to missing ones they appear in the fragment missing and they are updated when I select or deselect a number. The issue is that I have to reload the info between tags and it is encouraged when I reload the RecyclerView adapter and create all the elements. How can I improve this performance?

I use the following function to recreate the fragments when I change tabs but it is necessary because I update the base when I select an item and must refresh to see the changes between tabs,

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    //Fragment newFragment = new Fragment();
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser ) {
        //getFragmentManager().beginTransaction().replace(R.id.recyclerFaltantes,newFragment).commit();
        getFragmentManager().beginTransaction().detach(this).attach(this).commit();
    }
}

    
asked by Miguel Angel Castañeda 04.02.2018 в 19:41
source

1 answer

0

The idea when you handle tabs, is that the adapter is responsible for controlling how fragments are displayed on the screen. What this adapter does is create the fragments that are going to be displayed and keep them in such a way that it is not necessary to create them and destroy them periodically. With this in mind, what you do when you use the code lines

 getFragmentManager().beginTransaction().detach(this).attach(this).commit();

You are wasting memory when there is no need to do so. If what you want is to update the information in your second fragment you must do it in such a way that you do not have to delete it and then re-render the view.

What I recommend is to implement a fragment method that is responsible for changing the information that is shown in the fragment and call that method using the listener that you have already implemented to detect that a fragment is displayed on the screen.

I hope this answer is helpful.

    
answered by 05.02.2018 в 15:42