Sobes.tech
Junior

How to call a method in an activity from a fragment?

sobes.tech AI

Answer from AI

You can get a reference to the parent activity using the getActivity() method and then call the desired method.

// Inside Fragment

Activity activity = getActivity();
if (activity instanceof MainActivity) { // Check that the activity is of the correct type
    ((MainActivity) activity).myMethodInActivity();
}
How to call a method in an activity from a fragment… - sobes.tech