Reputation: 66717
How to pass, at any time!, a boolean field from one activity to a class?
Upvotes: 0
Views: 3582
Reputation: 36045
You can create your own singleton class that both your Activity and other class can access at any time. You have to be careful with it because it does add a layer of global variables (which people tend to not like), but it works.
public class MyBoolean{
private static final MyBoolean instance = new MyBoolean();
private boolean boolValue = false;
private MyBoolean(){}
public static MyBoolean getInstance(){
return instance;
}
public boolean getValue(){
return boolValue;
}
public void setValue(boolean newValue){
boolValue = newValue;
}
}
Call MyBoolean.getInstance()
and you can use the methods inside which will be in sync with your whole program.
Upvotes: 2
Reputation: 4887
Pass to Activity:
Intent i = new Intent(getBaseContext(), NameOfActivity.class);
i.putExtra("my_boolean_key", myBooleanVariable);
startActivity(i)
Retrieve in Second Activity:
Bundle bundle = getIntent().getExtras();
boolean myBooleanVariable = bundle.getBoolean("my_boolean_key");
Upvotes: 3