简体   繁体   中英

How call method from Fragment in Activity?

I have a class MainActivity with three fragments.

Fragment Two uses a WebView. I want to create a method Webview.reaload() in Fragment Two and call this method from MainActivity. How can I do that?

With getActivity() you will get the activity instance, cast it to your activity name and call function from fragments. Assuming activity name as MainActivity

((MainActivity)getActivity()).myFuncInActivity():

Interfaces work well will this kind of communication.

Check out this guide: https://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM