Reputation: 6220
I have 2 fragments and I would like to check from Fragment 1 to see if CheckBox1 in fragment 2 is checked.
EDIT
I am doing the following: In the main Activity:
@TargetApi(11)
public class gamesmodestab extends Activity{
public static Context appContext;
public boolean lf_ch=false;
public void onCreate(Bundle savedInstanceState){
appContext=this;
super.onCreate(savedInstanceState);
//Then I declare the fragments and add them
In the Frgment were the checkbox is available:
@TargetApi(11)
public class tabquests extends Fragment{
public CheckBox lc,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)//onCreateView
{
View V=inflater.inflate(R.layout.tab_quests, container, false);
lc=(CheckBox)V.findViewById(R.id.CheckBox01);
lc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(lc.isChecked())
lf_ch=true;//Wrong
else
lf_ch=false;//Wrong
}
});
In the fragment where I want to read lf_ch;
public void onClick(View v) {
if(lf_ch==false)//Wrong
{
This is in high level what I want to do. So I would like a way to access lf_ch set on Activity or to directly access the checkbox in the 2nd fragment.
Upvotes: 2
Views: 2240
Reputation: 1628
You should not be directly accessing the state of an input in one fragment from another.
It is better to create a activity level variable to save the boolean value indicated by the cb.
public boolean lf_ch = false; // default checked state.
Initialize the checked state of your cb to the value of your variable.
lc.setChecked(context.getActivity().lf_ch);
Then change the value of your variable when the checked state changes.
lc.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
context.getActivity().lf_ch = isChecked;
}});
Then in frag1 you can test the value of If_ch;
if(context.getActivity().If_ch){
// Do something
}
----- EDIT -------
@TargetApi(11)
public class gamesmodestab extends Activity{
public static Context appContext;
public boolean lf_ch=false;
public void onCreate(Bundle savedInstanceState){
appContext=this;
super.onCreate(savedInstanceState);
//Then I declare the fragments and add them
In the Frgment were the checkbox is available:
@TargetApi(11)
public class tabquests extends Fragment{
gamesmodestab gmt = (gamesmodestab) getActivity();
public CheckBox lc,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)//onCreateView
{
View V=inflater.inflate(R.layout.tab_quests, container, false);
lc=(CheckBox)V.findViewById(R.id.CheckBox01);
lc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
gmt.If_ch = isChecked;
}
});
You will need to define gmt in this class as well.
public void onClick(View v) {
if(!gmt.lf_ch)
{
Upvotes: 2