简体   繁体   中英

Access fragment's variable outside the fragment

I have a single activity, multiple fragments and multiple subview. In some cases I have to access a fragment's variable to which the subview(extends RelativeLayout) is attached. How can I do this?

I can access the views by

getActivity().getFragmentManager.getFragmentByTag("TAG").getView().findViewById()

But can't access the variables in that fragment.

getFragmentByTag returns a Fragment , try casting the fragment you get back to your implementation's class and then accessing them like so:

((MyCustomFragment)getActivity().getFragmentManager.getFragmentByTag("TAG"))
    .getMySuperSecretField();

If you post some more details/code, I might be able to diagnose you issue better

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