Asynchronous FragmentManager commits

I know this problem was asked before, but I could not find any useful answer for my problem. Imagine an application that uses the Google.Volley library for a RESTful api. When the application launches MainActivity it shows dialogFragment (Download) and when the answer comes, I would like to commit the FragmentTransaction and show the data to the user like this.

JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, parameterJson, new Response.Listener<JSONObject>()
{
    @Override
    public void onResponse(JSONObject response)
    {//parse data and present them

        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.container, MainScreenFragment.newInstance(gaObservers))
                .commit(); //i know that i can use commitAllowingStateLoss but app crashes on rotation change because of IllegalStateException: Activity has been destroyed
    }
}, new Response.ErrorListener()
{
    @Override
    public void onErrorResponse(VolleyError error)
    {
    }
});

So what's best here please? Any help would be appreciated. PS: I read Fragment Reports and Activity Loss Message , but I feel that I can’t avoid asynchronous commit. Thanks you

+4

All Articles