Ishaan Garg Ishaan Garg - 4 months ago 23
Android Question

Fragment not replacing in Volley's OnResponse()

I am trying to replace the fragment in Volley's onResponse(). It's getting replaced, but in a weird way.


  • The old frag is still visible while the new one isn't.

  • The old frag is non interactive & click events are being handled by
    the new frag.



It's like the new frag is beneath the old frag.

If I try to replace the frag outside Volley's onResponse(), then everything works the way it should: the old frag goes away showing the new one.

getNetworkManager().jsonGETRequest(new NetworkManagerRequestData(getActivity(), this,
orderListUrl,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_frame, IdleFragment.newInstance());
transaction.commit();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
..
}
}, NetworkManager.getDefaultPolicyForNonTransactions(), false));


The fragments are being added dynamically using a FrameLayout. I have an activity with 5-6 frags. Everything is working beautifully with animations, except this transaction.

Here's the R.id.main_frame which is an empty FrameLayout.

<FrameLayout
android:id="@+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
..
/>


UPDATE
So I tried implementing the fragment transaction inside an AsyncTask

new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_frame, IdleFragment.newInstance());
transaction.commit();
}
}.execute();


This has same behaviour. Normally transacting outside any async callbacks is working fine though.

Here is the layout of the frag i am trying to replace

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/pitch_grey"
android:clickable="true"
android:fillViewport="true">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
.....
</RelativeLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>

Answer

The problem is the Swipe Refresh Layout. When you are trying to replace the fragment while its refreshing, then the previous fragment will freeze. This is an issue in the SwipeRefreshLayout itself.

You can wrap your swipeRefreshLayout inside FrameLayout. This might work.

<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/pitch_grey"
    android:clickable="true"
    android:fillViewport="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
           .....
    </RelativeLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>

Ref: When switch fragment with SwipeRefreshLayout during refreshing, fragment freezes but actually still work