androidandroid-fragmentsandroid-volley

Fragment wont Switch to another Fragment after Using Volley in Android Studio [Java]


Hi im trying to send string request via Volley it works but it wont switch my fragment to another fragment here my code :

I'll simplify my code because its more than 900 lines

FragmentOrdertoCart.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".FragmentOrdertoCart"
    android:id="@+id/ContainerOrdertoCart">
    
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/ScrollViewOrdertoCart">
    <LinearLayout...>
    </ScrollView>
    <LinearLayout...>
</FrameLayout>

String Request of Volley

FragmentOrdertoCart.java

StringRequest stringRequest = new StringRequest(Request.Method.POST, URLs.ORDER,
                                new Response.Listener<String>() {
                                    @Override
                                    public void onResponse(String response) {
                                        try {
                                            //convert response to json object
                                            JSONObject obj = new JSONObject(response);
                                            if (!obj.getBoolean("error")) {
                                                Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();

                                                Fragment fragmentNotif = new StatusFragment();
                                                FragmentTransaction transaction0 = getFragmentManager().beginTransaction();
                                                transaction0.replace(R.id.ContainerOrdertoCart, fragmentNotif);
                                                transaction0.addToBackStack(null);
                                                transaction0.commit();

                                            } else {
                                                Toast.makeText(getContext(), obj.getString("message"), Toast.LENGTH_SHORT).show();

                                            }

                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                },
                                new Response.ErrorListener() {
                                    @Override
                                    public void onErrorResponse(VolleyError error) {
                                        Toast.makeText(getContext(),error.getMessage(),Toast.LENGTH_SHORT).show();
                                    }
                                }) {
                            protected Map<String, String> getParams() throws AuthFailureError {
                                Map<String,String> params = new HashMap<>();
                                params.put("notable",NotableGuest.toString());
                                params.put("tablecode",TableCodeGuest.toString());
                                params.put("ordercode",OrderCode.toString());
                                params.put("productcode",FoodCode.toString());
                                params.put("productqty",TxtvSelectedFoodCount.getText().toString());
                                params.put("toppingcode1",ToppCode1.toString());
                                params.put("toppingcode2",ToppCode2.toString());
                                params.put("toppingcode3",ToppCode3.toString());
                                params.put("toppingcode4",ToppCode4.toString());
                                params.put("toppingcode5",ToppCode5.toString());
                                params.put("toppingcode6",ToppCode6.toString());
                                params.put("toppingcode7",ToppCode7.toString());
                                params.put("toppingcode8",ToppCode8.toString());
                                params.put("toppingcode9",ToppCode9.toString());
                                params.put("toppingcode10",ToppCode10.toString());
                                return params;
                            }
                        };
                        VolleySingleton.getInstance(getContext()).addToRequestQueue(stringRequest);

and then

Fragment_Status.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".StatusFragment"
    android:id="@+id/ContainerStatus">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="@color/black"
        android:textSize="20sp"
        android:gravity="center"
        android:text="Status" />

</LinearLayout>

StatusFragment.java

package com.example.pesanpalgading20;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link StatusFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class StatusFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public StatusFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment StatusFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static StatusFragment newInstance(String param1, String param2) {
        StatusFragment fragment = new StatusFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View viewRoot = inflater.inflate(R.layout.fragment_status, container, false);
        // Inflate the layout for this fragment
        return viewRoot;
    }
}

So I want to switch between FragmentOrdertoCart to StatusFragment after I'm done completing String Request, But it just showing toast message of succesfully doing String Request and won't switch to StatusFragment


Solution

  • try getSupportFragmentManager() instead of getFragmentManager().
    If this fragment is inside another fragment use getChildFragmentManager()