简体   繁体   中英

Android: Getting instance of my Activity from Fragment

I have Activity and Fragment.

I can access Activity using method getActivity(); But it's instance of FragmentActivity. How can I get instance of my Activity?

I tried it: ((MyActivity) getActivity())

It works but I think it's not the best way to do it. Is there more good ways?

Following ways are there.

  1. In onCreateView have catch the reference of the view , then view.getContext();
  2. getView() can be used, getContext() concept;

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