0

I want after directing the user to the preferences window "setting", to have an indicator implies whether or not the user returned back from the preferences activity whether he ticked a designated option or not.

I know that I can start the preferences activity"child activity" from my current activity "Parent", using startActivity() or startActivityForResult(). but in this case I want to check the status of the returning back from the child activity

UPDATE

Or to be more specific, I want to check the if the "gps enable" option which resides in the "Location Services" menu is ticked or not.

  • in your onActivityResult, after you made sure the app is back from the pref. activity, check the SharedPreference used by the PreferenceActivity to see whether the user updated the value by ticking the checkbox. – rekaszeru May 18 '14 at 05:27
  • can you please provide some examples as for checking the system sharedpreferences? –  May 18 '14 at 05:39
  • please see the explanation and the samples in the answer below on how to use the checkbox preference. – rekaszeru May 18 '14 at 06:21

1 Answers1

0

Let's assume you have a CheckboxPreference in your PreferenceActivity declared as:

<CheckBoxPreference
    android:defaultValue="false"
    android:key="prefAutoLogin"
    android:summary="@string/pref_autologin_summary"
    android:title="@string/pref_autologin" />

In your main activity's onOptionsItemSelected you check which menu item was pressed, and in case of the settings menu, you start your PreferenceActivity:

final Intent i = new Intent(this, MySettingActivity.class);
startActivityForResult(i, RESULT_SETTINGS); 
// RESULT_SETTINGS is an integer constant to identify the preference activity

You start the MySettingsActivity for result, meaning the current activity will be notified when it returns in the onActivityResult method. You overwrite that method to check for the modifications:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) 
    {
    case RESULT_SETTINGS:
        // here you check whether the auto-login preference was checked or not:
        final SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(this);
        final boolean autoLoginChecked = prefs.getBoolean("prefAutoLogin", false);
        // prefAutoLogin is the key of your CheckBoxPreference declared in the xml above.
        if (autoLoginChecked)
        {
            // perform the actions necessary when the checkbox is ticked
        }
        else 
        {
            // ...
        }
        break;
    }
    [...]
    default {...} 
}

If you need to perform some actions only when the checkbox was ticked from false to true (and not every time it returns from the pref. activity with a checked state), you should store its state prior to start the preference activity, and match against the new state on return:

private boolean initiallyChecked; 

and before you start the settings activity:

final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
initiallyChecked = prefs.getBoolean("prefAutoLogin", false);

Then when the activity returns back, you don't just check if the checkbox value is true, but check to see whether it was false before:

if (autoLoginChecked && !initiallyChecked)
{ .... }

For more reference see this post on viralpatel.

rekaszeru
  • 18,741
  • 6
  • 55
  • 72
  • 1
    With your update the question just got another meaning, please see this thread on how to achieve your goal: http://stackoverflow.com/a/7990939/506879 – rekaszeru May 18 '14 at 06:31
  • @Elpharaoh in the future please avoid such updates that change your question completely. And make sure you search for it first, since both of your questions have been answered several times before. – rekaszeru May 18 '14 at 06:40