Fragment not attached to an activity - Problem with Dialog Fragment and async calls

154 Views Asked by At

I am facing an issue, that does not occur often, but sometimes i get this report in Crashlytics. The problem which is described below, happens sometimes. I can not reproduce it while debugging.

FIrst of all let me describe the patern. In my activity i display a custom dialog fragment when some conditions are met. In this dialog, i prompt the user to enter some data. If the user enters data and clicks "ok", i make an async http call using retrofit 2 (call.enque), in order to post the data to the server. The class which is responsible for the retrofit call, makes a callback to the fragment, in order to inform it, that the call was finished, regardless if the call was successful. When that happens ,either i display o Toast in the fragment or i call a method from fragment's hosting activity. In both of these cases, i use requireActivity() and i rarely get the following exception.

java.lang.IllegalStateException: Fragment MyCustomDF{6b73c58} (9c5f953b-98c3-4b6f-a935-1ceb7d488ee0) not attached to an activity.
androidx.fragment.app.Fragment.requireActivity(Fragment.java:998)

Below is the way i display fragment in host activity.

    private void displayDialog(){
       FragmentManager fragmentManager = getSupportFragmentManager();
       myCustomDF = (MyCustomDF) fragmentManager.findFragmentByTag("My TAG");
       if (myCustomDF != null) {
           myCustomDF .dismiss();
       }
       Bundle bundle = new Bundle();
       bundle.putInt(FinalValues.ITEM, item);
       bundle.putInt(FinalValues.QTY, qty);
       bundle.putInt(FinalValues.TYPE, Type);
       bundle.putString(FinalValues.CALLING_ACTIVITY,"MyActivity");
       myCustomDF = new MyCustomDF();
       myCustomDF .setArguments(bundle);
    
       myCustomDF .show(fragmentManager, "My TAG");}

The fragment

 positiveBT.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if( (item!=null) && (!qty1.getText().toString().equals(""))  && (Integer.parseInt(qty1.getText().toString().trim())>0) ){
                setProgressMessage("Posting data to server");
                showProgressBar();
                SetDataToServer setData= new SetDataToServer(requireActivity().getApplicationContext(), new InsertCallBack() {

                    @Override
                    public void OnSuccessInsert() {
                        hideProgressBar();
                        Toast.makeText(requireActivity().getApplicationContext(),"Success post to the server",Toast.LENGTH_LONG).show();
                        requireDialog().dismiss();
                    }

                    @Override
                    public void OnFailInsert(String result) {
                        hideProgressBar();
                        Bundle bundle1 = new Bundle();
                        bundle1.putString(FinalValues.FRAGMENT_MESSAGE,result);
                        errorDF.setArguments(bundle1);
                        errorDF.show(requireActivity().getSupportFragmentManager(),FinalValues.ERROR_DF);
                        requireDialog().dismiss();
                    }
                });
                setData.setSub(url,authClientID,substitute,item,Integer.parseInt(qty1.getText().toString().trim()));
            }
            else{
                Toast.makeText(requireActivity().getApplicationContext(),"Fill the required fields",Toast.LENGTH_LONG).show();
            }
        }
    });

And a way to call a method to hosting activity from the fragment after an async call

              ((MyActivity)requireActivity()).myMethod(itemCode.getText().toString(),itemName.getText().toString(),item,0,qty1);

So could you please help me? is there anyway to detect if fragment is attached to the acitivity. If is not attahed, can i reattach the fragment to its hosting activity?

0

There are 0 best solutions below