Android: how can fragment take a global variable of Activity

Either set the var as public static, or use

((MyActivity)getActivity()).getX()

Using a public static variable isn't the best way to communicate between an activity and a fragment. Check out this answer for other ways:

The Android documentation recommends using an interface when the Fragment wants communicate with the Activity. And when the Activity wants to communicate with the Fragment, the Activity should get a reference to the Fragment (with findFragmentById) and then call the Fragment's public method.

The reason for this is so that fragments are decoupled from the activity they are in. They can be reused in any activity. If you directly access a parent Activity or one of its global variables from within a fragment, you are no longer able to use that fragment in a different Activity.