I want to call the method of activity in the fragment

Asked 2 years ago, Updated 2 years ago, 149 views

Fragment is trying to invoke the method of activity. After you turn over the data in the fragment and do something in the method, I want to return the value, but I want to make it like a static method, but I can't use the static method because I think there will be a problem with the activity. Is there any other way?

android android-fragments

2022-09-22 22:10

1 Answers

From Fragment (Activity class name) getActivity().public method of activity(); You can do it like this.

If you want to invoke the method of fragment in the activity

FragmentManager fm = getSupportFragmentManager();

//When you put the fragment in xml, 
Fragment class name fragment = (Fragment class name) fm.findFragmentById (R.id.fragmentClassID);
public method of fragment.fragment();

You can do it like this.


2022-09-22 22:10

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.