在主类中从静态片段类调用方法 [英] Call method in main class from static fragment class

查看:155
本文介绍了在主类中从静态片段类调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段类,我想在我的活动的tgemain类中调用一个方法。片段类是静态的,所以这可能是什么导致问题,虽然我不得不保持静态。我想能够从我的静态类中做这样的事情:
Method();
我试过:
getActivity()。Method();
虽然没有工作。我应该怎么办?

解决方案

您应该能够将返回的活动投射到您的特定类以访问公共方法。 / p>

如果您的主类名为 MainActivity ,并且您有一些公共方法方法,那么您可以从fragment方法:

 ((MainActivity)getActivity())。 

或者,您可以使用事件回调模式


I have a fragment class and I want to call a method in tge "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?

解决方案

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.

这篇关于在主类中从静态片段类调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆