I know this issue was asked before, but I couldn't find any usable answer for my problem. Imagine application, which uses Google.Volley library for RESTful api. When app launches MainActivity shows dialogFragment(Loading) and when response comes, I would like to commit FragmentTransaction, and show 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 is best practice here please? Any help will be appreciated. PS: I have read Fragment Transactions & Activity State Loss blog post, but I feel like I cant avoid async commit. Thank you