5

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

Before calling the method these method calls return expected values.

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);
        }
}
K.Os
  • 5,123
  • 8
  • 40
  • 95
Aalap Patel
  • 2,058
  • 2
  • 17
  • 31

0 Answers0