How to access Activity Variables from a fragment Android

Syntax_Error picture Syntax_Error · Oct 25, 2012 · Viewed 71.8k times · Source

In the Activity I have :

public class tabsmain extends Activity{
    public static Context appContext;

    public boolean lf_ch=false;

    public void onCreate(Bundle savedInstanceState){

I would like to access and possibly change lf_ch from a fragment inside tabsmain;

public class tabquests extends Fragment{ 
    public CheckBox lc;
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)//onCreateView
    { 
lc.setChecked(//set it to lf_ch);

However, I can't seem to access the value of lf_ch.

Answer

David M picture David M · Oct 25, 2012

Try this:

public View onCreateView(...){
  tabsmain xxx = (tabsmain)getActivity();
  lc.setChecked(xxx.lf_ch);
}