0

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.

James Z
  • 12,209
  • 10
  • 24
  • 44
Gaurav
  • 13
  • 4

2 Answers2

0

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.

0

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();
Bhoomi Vaghasiya
  • 311
  • 1
  • 5
  • 12