The snippet isAdded returns false and isAttached returns true

I call a method in an activity when I am on one of the fragments and then return the result of the method to the activity on the same fragment I am on, but surprisingly this gives me an error since the fragment has not been added yet. I am printing the isAttached and isDetached values ​​and the true and false values ​​respectively. Also getContext returns null. How is this possible?

This method calls the return of the expected values ​​before calling the method.

Fragment.class
public class myFragment extends Fragment{
    @Override
    public void onDialogInteraction(String value) {
      super.onDialogInteraction(value);
      Log.d(TAG, "onDialogInteraction: value "+value);//prints correct value
      Log.d(TAG, "onDialogInteraction: iaAdded "+isAdded()); false
      Log.d(TAG, "onDialogInteraction: isAttahced "+isAttached());true
      Log.d(TAG, "onDialogInteraction: isDetached "+isDetached());false
      Log.d(TAG, "onDialogInteraction: isDetached "+getContext());null

    }
}

public class MyActivity extends AppcompatActivity{
     @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
            String value = getValue(intent);
            myFragmentInstance.onDialogInteraction(value);
        }
}

      

+3


source to share





All Articles