Reputation: 185
i'm saving the users login information to SharedPreferences, so he only has to configure his login data once.
This is my onBackPressed method in my Preferences.class (extends PreferenceActivity):
@Override
public void onBackPressed() {
//Login again
Intent intent = new Intent(Preferences.this, LoginActivity.class);
startActivity(intent);
}
What I need is a if-condition which checks, if the preferences changed or not.:
If the user opens the Preferences Activity (edit: from any View(!)), and does not change anything and clicks the backbutton -> just go back to last state.
If the preferences changed: call LoginActivity.
Couldnt find a solution yet and the LoginActivity gets called whenever i hit the backbutton.
Thanks in advance, Marley
Upvotes: 0
Views: 153
Reputation: 41099
To determine if there was a change in SharedPreferences
you have to assign a OnSharedPreferenceChangeListener
to your SharedPreferences
object like this:
prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.registerOnSharedPreferenceChangeListener(this);
in this case I'm doing it in my Application
class that's implementing:
public class YambaAppObj extends Application implements OnSharedPreferenceChangeListener
Then you will have to override:
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
//this method will be called when preferences are changed.
//do here what you want to record the change
Log.d(TAG , "onSharedPreferenceChanged for:" + key);
}
and then you could check this record in your onBackPressed()
method of your Preferences Activity
and act accordingly.
Upvotes: 1
Reputation: 75629
You can use OnSharedPreferenceChangeListener
or (depending on what you really meany by changed
) you can try utilising onSharedPreferenceChanged()
like:
protected Boolean mPrefsChanged = false;
@Override
public void onSharedPreferenceChanged( SharedPreferences sharedPreferences,
String key ) {
mPrefsChanged = true;
}
of course it's far from perfect, but at least you got more options
Upvotes: 1