添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I have implemented fragment in viewpager and Fragment has some buttons. Also viewpager is in activity_main layout.

I want that when button is clicked then it implement a method which is mentioned in mainActivity.java.

How can I do this?

I am a beginner.

You can get MainActivity method from fragment like below. Make sure your methid is public if MainActivity and ViewPager are not in same package.

((MainActivity) getActivity()).getMethodOfMain();

You can call getActivity Method from your fragment and cast it to the your respective Activity.

lets take an example : You Have MainActivity class

MainActivity { 
public void check() {}

And you have fragment : MainFragment

MainFragment{ 
Activity mainActivity  = (MainActivity) getActivity();
mainActivity.check();

Thats how you can call the Respective Activity Method.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.