java.lang.IllegalStateException: FragmentManager has been destroyed

Sergey Roytman :

In activity onResume method I call volley request, which is getting list of items and then loads them to ListFragment inside this activity. When I enter activity for the first time everything is working correctly, but when I re-enter activity the ListFragment is empty and there is message in console "FragmentManager has been destroyed".

This is my activity's code:

@Override
protected void onResume() {
    super.onResume();
    // Volley request inside - which call back albumsFoundViewUpdate
    artistController.getArtistAlbums(artist);
}

public void albumsFoundViewUpdate(ArrayList<UserAlbumLink> links)
{
    // Load list fragment
    UserAlbumLinkListFragment fragment = new UserAlbumLinkListFragment(links, artist);
    getSupportFragmentManager().beginTransaction().replace(R.id.artist_activity__albums_container, fragment).commit();
}

The exception is thrown at commit() statement.

My ListFragment code:

public class UserAlbumLinkListFragment extends ListFragment {
    private List<UserAlbumLink> albumLinks;
    private UserAlbumLinkListAdapter adapter;
    private Artist artist;


    public UserAlbumLinkListFragment() {
    }

    public UserAlbumLinkListFragment(List<UserAlbumLink> albumLinks, Artist artist) {
        this.albumLinks = albumLinks;
        this.artist = artist;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        adapter = new UserAlbumLinkListAdapter(getActivity(), R.layout.album_list_item, albumLinks, artist);
        setListAdapter(adapter);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_album_list, container, false);
    }
}

As I understand FragmentManager gets destroyed when activity is getting destroyed, but how do I get new FragmentManager for recreated activity? Why getSupportFragmentManager() is not working?

Sergey Roytman :

I totally didn't get why did it worked, but I've solved the problem. I've created static activity variable and initiate it with this in onCreate method:

private static ArtistActivity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    activity = this;
    ...
}

And I've noticed that in method albumsFoundViewUpdate this.isDestroyed() returns true (when I relaunch this activity for second time) and activity.isDestroyed() returns false. So I've remake albumsFoundViewUpdate like this:

public void albumsFoundViewUpdate(ArrayList<UserAlbumLink> links)
{
    // Load list fragment
    UserAlbumLinkListFragment fragment = new UserAlbumLinkListFragment(links, artist);
    FragmentManager fragmentManager = activity.getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.artist_activity__albums_container, fragment).commit();
}

and now it's working perfectly fine every time!

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=415404&siteId=1