Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call method in main class from static fragment class

I have a fragment class and I want to call a method in the "main" class of my activity. The fragment class is static so that is probably whats causing the problem although I have to keep it static. I want to be able to do something like this from inside my static class: Method(); I've tried: getActivity().Method(); Although that didn't work. What should I do?

like image 695
SweSnow Avatar asked Jan 15 '23 19:01

SweSnow


1 Answers

You should be able to cast the activity returned to your specific class to access the public methods.

If your main class is called MainActivity and you have some public method Method then you could do the following from your fragment method:

((MainActivity) getActivity()).Method();

Alternatively you could use the event callback pattern described in the fragment documention.

like image 90
Error 454 Avatar answered Jan 23 '23 05:01

Error 454