4

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

beretis
  • 899
  • 1
  • 9
  • 24
  • What error messages do you get when you use `commit()`? Do you get error messages when you use `cocmmitAllowingStateLoss()`? – Alex Lockwood Nov 12 '14 at 03:01
  • whit commit() im getting "Can not perform this action after onSaveInstanceState" and when i use commitAllowingStateLoss(), app doesn't crash when phone locks, but on orientation change, because i call async request in OnCreate(), I'm getting IllegalStateException: Activity has been destroyed....Thank you for your time! – beretis Nov 12 '14 at 09:07
  • 2
    @VamsiChalla I reorganized my code, so I did that loading in Activity, and after loading I create Fragments and display data. – beretis Jan 13 '15 at 12:00
  • @beretis, Yeah even i changed the fragment transaction code to oncreate and now its working fine. Thanks!!! – Vamsi Challa Jan 13 '15 at 12:21

0 Answers0