Reputation: 79
Anyone knows how to save a button's state? I want to enable a button which will be enabled permanently based on certain condition, it should remain enabled even the user exits the application. I already did the enabling of the button. Now how can I save it to make it enabled all through out?
I am setting it enable this way. I am using putExtra on the other class to set a button enable.
Button page2 = (Button) findViewById(R.id.button2);
Intent intent2=getIntent();
String isEnabled2 = intent2.getStringExtra("isEnabled2");
if(isEnabled2==null||isEnabled2.equals("disabled")){
page2.setEnabled(false);
}
else{
page2.setEnabled(true);
}
page2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), p3.class);
startActivityForResult(myIntent, 0);
}
});
EDIT
Button page2 = (Button) findViewById(R.id.button2);
Intent intent2=getIntent();
String isEnabled2 = intent2.getStringExtra("isEnabled2");
if(isEnabled2==null||isEnabled2.equals("disabled")){
page2.setEnabled(false);
}
else
{
page2.setEnabled(true);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(Enable.this);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("Name",true); //name is the key so may use a username or whatever you want
editor.commit();
}
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean btnEnabled = preferences.getBoolean("Name",false); //false because you probably want that as your default value
if(btnEnabled)
{
page2.setEnabled(true);
}
page2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), p3.class);
startActivityForResult(myIntent, 0);
}
});
Upvotes: 1
Views: 2893
Reputation: 479
A better way to save view and other types states would be via the onSavedInstanceState(Bundle state) method and restoring it through the onRestoreInstanceState(Bundle state) method. These both will need to be overriden.
the first method is where you save a state using the bundle state:
state.putString("A KEY ID", "KEY Value");
This is one example on how to save a string. You can save many different types such as boolean, bundles, serializable's etc. Just see whats available using 'state.put' and it should show you a list of methods you can use depending on your IDE.
Then you can restore the state from the second method, onRestoreInstanceState doing what you need to do with the information you have saved, such as, setting text on a view, or displaying the users saved name.
If you got any questions let me know
Upvotes: 0
Reputation: 44571
You can save the state in Shared Preferences then check against the value when the user open the app. Another option would be to store it in SQLite DB if you have a lot of other data that needs to be stored for the user
There are tons of examples on using Shared Prefs so instead of writing another example here is a good one on SO
Example
if(isEnabled2==null||isEnabled2.equals("disabled")){
page2.setEnabled(false);
}
else{
page2.setEnabled(true);
//create an editor and put your value in there
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(YourActivity.this);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("Name",true); //name is the key so may use a username or whatever you want
editor.commit();
}
Then when you want to retrieve this value
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
boolean btnEnabled = preferences.getBoolean("Name",false); //false because you probably want that as your default value
if(btnEnabled)
{
page2.setEnabled(true);
}
Something like this should work. I haven't checked it but it should get you going
Edit
To have it enabled to begin, in xml:
android:clickable="false"
or programmatically set it in onCreate()
before checking shared prefs
page2.setEnabled(false);
Upvotes: 2