Friday, 5 November 2021

Fragment retrieved from FragmentManager sometimes has its RecyclerView Adapter set to null

In my application I have one Fragment which is responsible for displaying a list of news items. It takes a String parameter which determines which url to pull data from.

I set the Fragment with this code:

private void setFragment(String pageToLoad, NewsFeedFragment newsFeedFragment) {

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    if(newsFeedFragment == null) {
        transaction.replace(R.id.container, NewsFeedFragment.newInstance(pageToLoad), pageToLoad);
    }
    else {
        transaction.replace(R.id.container, newsFeedFragment, pageToLoad);
    }
    mPageToLoad = pageToLoad;

}

In my parent Activity I keep track of which 'page' is currently being viewed:

protected void onSaveInstanceState(@NonNull Bundle outState) {
    if(mPageToLoad != null) {
        outState.putString("pageToLoad", mPageToLoad);
    }
    super.onSaveInstanceState(outState);
}

In my parent Activity onCreate method I check whether an instance of NewsFeedFragment has been created and added to the FragmentManager as follows:

protected void onCreate(Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        if (savedInstanceState.containsKey("pageToLoad")) {
            String pageToLoad = savedInstanceState.getString("pageToLoad");
            if(pageToLoad != null) {
                NewsFeedFragment newsFeedFragment = (NewsFeedFragment) getSupportFragmentManager().findFragmentByTag(pageToLoad);
                if(newsFeedFragment != null) {
                    setFragment(pageToLoad, newsFeedFragment);
                }
                else {
                    setFragment(pageToLoad, null);
                }
            }
        }
    }
}

This works well 99% of the time, the application resumes correctly and displays the last instance of NewsFeedFragment added. However, I have an issue which seems to occur randomly where the RecyclerView Adapter in NewsFeedFragment is sometimes null when the Fragment is retrieved from the FragmentManager using the findFragmentByTag(pageToLoad) method.

In NewsFeedFragment the RecyclerView Adapter is a class variable:

public NewsPageAdapter mNewsPageAdapter;

The onActivityCreated method of NewsFeedFragment is as follows:

public void onActivityCreated(Bundle savedInstanceState) {

    if(mNewsPageAdapter == null) {
        Log.i(TAG, "mNewsPageAdapter is null");  // This is logged when issue occurs
    }
    if(savedInstanceState == null || mNewsPageAdapter == null) {
        new LoadFirstPageTask().execute();  // Fetches news items from web service, creates mNewsPageAdapter, and then calls setupRecyclerView() method
    }
    else {
        setupRecyclerView(savedInstanceState);
    }

}

Finally, this is the NewsFeedFragment setupRecyclerView method:

private void setupRecyclerView(Bundle savedInstanceState) {
    mRecyclerView.setLayoutManager(mLayoutManager);
    mRecyclerView.setAdapter(mNewsPageAdapter);
}

From what I've described could anyone offer any insight as to why the NewsPageAdapter may sometimes be null when the Fragment is retrieved from The FragmentManager?

Thanks



from Fragment retrieved from FragmentManager sometimes has its RecyclerView Adapter set to null

No comments:

Post a Comment