How to access Activity Variables from a fragment Android
JavaAndroidAndroid ActivityFragmentJava Problem Overview
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.
Java Solutions
Solution 1 - Java
Try this:
public View onCreateView(...){
tabsmain xxx = (tabsmain)getActivity();
lc.setChecked(xxx.lf_ch);
}
Solution 2 - Java
I know this is an old question, however here is an easy answer that work without jumping through any hoops. In you Fragment define a variable that is the Activity that the fragment will be in then in the onCreateView connect the variable to the activity and then you have a reference that can get to any public variable in the main activity. I had forgotten it when I ended up here. It's a little hard to do the other way as you need to find the exact fragment that is showing. However with this you shouldn't need to do it the other way because you can easily pass things back and forth. I hope this help anyone that comes across it.
public Quiz_Avtivity mainQuiz;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_quiz, container, false);
mainQuiz = (Quiz_Avtivity) getActivity();
//Below is where you get a variable from the main activity
mainQuiz.AnyPublicVariable = whatEver;
//also
whatEver = mainQuiz.AnyPublicVariable
Solution 3 - Java
if you are using Java, you can use
((YourActivityName)getActivity()).variableName
to access, and if you are using Kotlin, you can use
(activity as YourActivityName).variableName
If the variable is defined as null in kotlin, you have to try each of these method as well:-
(activity as? YourActivityName).variableName
(activity as? YourActivityName)!!.variableName
or have to use let block, if possible.
Choose the correct one for you!
Hope, It will help.
Solution 4 - Java
#Make a generic Result receiver
You can create an interface for this task which would fetch a String data from any Activity to your Fragment. Follow these steps.
Create an interface
public interface MyResultReceiver{
public String getResult();
}
Make MyResultReceiver
a member of your Fragment
public class tabquests extends Fragment{
public CheckBox lc;
public MyResultReceiver resultreceiver;
@Override
public void onAttach(Context context){
super.onAttach(cotext);
resultreceiver = (MyResultReceiver)context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)//onCreateView
{
YourFragment code code
Boolean result = resultreceiver.getResult();
lc.setChecked(result);
}
}
Implement MyResultReceiver
in the Activity and override the method
public class tabsmain extends Activity implements MyResultReceiver{
public boolean lf_ch=false;
// Activity code
@Override
public boolean getResult(){
return lf_ch;
}
}
##Disclaimer:
You might find it a bit lengthy for this case. But the plus point of this approach is that if you want to reuse this code for another activity. You will not have to write the same logic again. Just implement the MyResultReceiver
in your activity , override the method and your will be good to go.
TIP: To be able to get any kind of data, change the method definition in the interface
from public String getResult();
to public Object getResult();
Solution 5 - Java
take Activity value in fragment.
((MainActivity) getActivity()).mGoogleApiClient;
Solution 6 - Java
Another way to get data from activity is to access activity's intent via:
getActivity.getIntent().getExtras();
and etc.
It can be useful if you starts activity with fragment in xml, and would like to control somehow fragment's onCreate() behavior.
PS: of cause you should firstly put something to intent
Solution 7 - Java
You could try the following method:
lc.setChecked(((yourpackagename)getActivity()).lf_ch);
Solution 8 - Java
try tabsmain.appContext.lf_ch
will give u value of that variable.
Also in that activity set appContext = this
Solution 9 - Java
try this
public boolean lf_ch=false;
public class tabsmain extends Activity{
public static Context appContext;
public void onCreate(Bundle savedInstanceState){
Solution 10 - Java
Try Something like this:
ViewPager mViewPager = (ViewPager) getActivity().findViewById(R.id.m_view_pager);
Solution 11 - Java
Access Activity variables in fragment to use static keyword like this:
MainActvity.java
public static boolean lf_ch=false;
tabquestsFragment.java
boolean if_value=MainActvity.lf_ch;
I hope it helps you
Solution 12 - Java
note that your fragment loads before the activity. so, you have to call the
tabsmain tabsm=(tabsmain) getActivity();
line in onActivityCreated()
method
Solution 13 - Java
Solution : You can try this.
In tabquests Fragment use this,
public class tabquests extends Fragment{
private tabsmain tabsmainActivity;
public CheckBox lc;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup
container,Bundle savedInstanceState)//onCreateView
{
tabsmainActivity = (tabsmain)getActivity; //typecasting
//now you can access the variables of tabsmain activity and make
//sure you give them public access in the activity`
tabsmainActivity.lf_ch; //or do whatever operation you want here.
lc.setChecked(//set it to lf_ch);
}
Solution 14 - Java
Change:
public boolean lf_ch=false;
to: public static boolean lf_ch=false;
You can access/change the value with: tabsmain.lf_ch