I have a Fragment, which should execute a (public) Method reset() from the Activity in the Fragment it is called from.
I found, for example, this page:
Calling Activity methods from Fragment
The other way there’s no problem:
Input_tap Input_tap = (Input_tap) getFragmentManager().findFragmentById(R.id.input);
Input_tap.reset();
Does someone has any idea how to do it the other way?
and: Does the Method has to be public – its a “void” Method?
A
Fragmentcan callgetActivity()to retrieve the activity that is hosting it.If there is only one possible activity that can host the fragment, just cast the
Activityto the proper class (e.g.,MyActivity) and call a method on it:If there might be more than one activity that can host the fragment, you are best served by implementing a common
interfaceon all those activities, so you can castgetActivity()‘s result to that interface: