Which lifecycle method should be used to implement Fragment Recyclerview: onCreateView, onViewCreated or onActivityCreated? -> Deciding the appropriate lifecycle method for implementing Fragment Recyclerview: onCreateView, onViewCreated or onActivityCreated

Feedback


Question:

When setting up my RecyclerView, is it preferable to initialize it in onCreateView, onViewCreated, or another suitable method?

I am confused about which method to use among onCreateView, onViewCreated, and onActivityCreated. Despite searching for explanations, opinions vary as some suggest using onCreateView while others prefer onViewCreated or onActivityCreated. Additionally, I am unsure if onCreateView should only be used to inflate the layout.

The code I am referring to is mine.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_tab1, container, false);
    recyclerViewSongs = rootView.findViewById(R.id.recyclerViewSongs);
    initRecyclerView();
    Log.e(TAG, "onCreateView called!");
    return rootView;
}
private void initRecyclerView() {
    Main.musicList = Main.songs.songs;
    // Connects the song list to an adapter
    // (Creates several Layouts from the song list)
    allSongsAdapter = new AllSongsAdapter(getContext(), Main.musicList);
    final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
    recyclerViewSongs.setLayoutManager(linearLayoutManager);
    recyclerViewSongs.setHasFixedSize(true);
    recyclerViewSongs.setAdapter(allSongsAdapter);
    recyclerViewSongs.addOnItemTouchListener(new OnItemClickListeners(getContext(), new OnItemClickListeners.OnItemClickListener() {
            @TargetApi(Build.VERSION_CODES.O)
            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getContext(), "You Clicked position: " + position, Toast.LENGTH_SHORT).show();
                if (! Main.songs.isInitialized())
                    return;
                //Start playing the selected song.
                playAudio(position);
            }
        }));
}



Solution 1:

If you’re utilizing

Fragment

, then

onCreateView()

is the optimal selection. The contrast lies in how

onCreateView()

acts as the

Fragment

for Activities and runs during the

View

creation, while

onViewCreated()

executes after the

View

has been established.


onActivityCreated()

invokes the

onCreate()

method from

Activity

, and this operation is successfully executed as demonstrated in this example: https://stackoverflow.com/a/44582434/4409113


Solution 2:


To prevent sluggishness in the UI, it is recommended to set up the RecyclerView in onCreateView(), which is similar to onCreate() for Activities. Setting up the RecyclerView in onViewCreated() before populating the UI may cause sluggishness.

Frequently Asked Questions