Search code examples
javaandroidandroid-dialogfragment

Pass TextInputLayout or any UI components from Activity to DialogFragment


I have a registration activity and a dialog fragment. Every time I press a button from registration activity, a dialog fragment with a button shows up, web service is called when the button is pressed. What I want to do is, I want to get the TextInputLayout from registration activity to my dialog fragment because if there's a web service error in one of the fields I want to use setError() to display that error. I've tried using an LayoutInfater to inflate the view, i don't get any error when I run the code but I also don't see inline error in my TextInputLayout.

LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.activity_register, null);

Is it possible? Any help would be greatly appreciated.


Solution

  • You should never pass views from one activity/fragment to other activities/fragments!
    In your case, its better to use a mechanism to pass result of API call from your dialog fragment to activity. In this case there are multiple ways; one is to use a SharedViewModel between your dialog and activity. Another one is to use FragmentResult API to pass result of the API call to activity.
    You can read more about how to implement these here