Ishaan Garg
Ishaan Garg

Reputation: 3093

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.

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>

Upvotes: 0

Views: 490

Answers (2)

Sakshi Gupta
Sakshi Gupta

Reputation: 116

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

Upvotes: 2

Embydextrous
Embydextrous

Reputation: 1661

You cannot replace a fragment that is statically placed in an xml layout file. Is the idle fragment statically placed? You should create a container (e.g. a FrameLayout) in the layout and then add the fragment programatically using FragmentTransaction.

Also use an empty FrameLayout as fragment container. Avoid, using LinearLayout or RelativeLayout. I faced this problem once using LinearLayout and replaced LinearLayout with FrameLayout and it worked fine.

Another suggestion, avoid doing fragment transactions in asynchronous calls. It may cause IllegalState Exception. If you still want to do this, use commitAllowStateLoss instead of commit.

Upvotes: 0

Related Questions