0

I'm trying to learn how to develop Android apps. I followed a video tutorial on YouTube, and it ended by adding a simple App Settings screen to the application.

However, there's one point that bothers me: when I press the back button on my phone's navigation bar, the changed settings aren't applied.

I have tried searching on Google, but none of the solutions I found have worked. The fact that I don't yet understand 100% of what's happening on the proposed solutions may also contribute to my difficulty on solving this one problem.

The behavior I expect from the app is that when I press the back button on the navigation bar, the changed settings should be applied.

For instance, I have a setting for dark background, which is controlled by a checkbox. The current behavior is: I check the setting for dark background. When I press the back button on the navigation bar, the setting isn't applied (I do have a method that loads the preferences on my MainActivity). What I want to happen is when I press the back button, the dark background is applied in this case.

From what I understand, I believe that overriding onBackPressed should do the trick, but I don't know what should be executed in order to properly apply the settings.

Here are the class and layout of my PreferenceScreen. Regarding the strings on the XML, they aren't actually hard-coded. I just copied the English values here to show the text that should appear on the interface.

public class AppPreferences extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_note_detail);

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        SettingsFragment settingsFragment = new SettingsFragment();
        fragmentTransaction.add(android.R.id.content, settingsFragment, "SETTINGS_FRAGMENT");
        fragmentTransaction.commit();
    }

    public static class SettingsFragment extends PreferenceFragment
    {
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);

            addPreferencesFromResource(R.xml.app_preferences);
        }
    }
}

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="General">
        <EditTextPreference
            android:title="Notebook"
            android:summary="The title that will be used on the main action bar."
            android:key="title"
            android:defaultValue="Notebook" />
    </PreferenceCategory>

    <PreferenceCategory
        android:title="Color">
        <CheckBoxPreference
            android:title="Dark Background"
            android:summary="Is the main background color dark?"
            android:key="background_color"
            android:defaultValue="false" />
    </PreferenceCategory>
</PreferenceScreen>
2
  • Welcome to StackOverflow. Please read the guidelines of how to ask: stackoverflow.com/help/how-to-ask. You should include in your description what behaviour is expected. Also, a more relevant piece of code, as this sure doesn't give enough context of what you are trying to do.
    – Chisko
    Sep 7, 2016 at 14:34
  • Added some more description to the question
    – tady159
    Sep 7, 2016 at 14:40

3 Answers 3

0

You will need to use

public class AppPreferences extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {

    super.onCreate(savedInstanceState);

    //setContentView(R.layout.activity_note_detail);

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    SettingsFragment settingsFragment = new SettingsFragment();
    fragmentTransaction.add(android.R.id.content, settingsFragment, "SETTINGS_FRAGMENT");
    fragmentTransaction.commit();
}

public static class SettingsFragment extends PreferenceFragment
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.app_preferences);

        Preference preference = findPreference("background_color");
        preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {

                //do your action here

                return false;
            }
        });
    }
}    

}

Or from other activity:

PreferenceManager.setDefaultValues(this, R.xml.your_setting_xml, false);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);

if (settings.getBoolean("background_color", true)) {
 //do your action here

.........

Refer to this question also, (it has similar use case): Checkbox Preference and Checking if its enabled or disable

3
  • Thank you, I'll give this a try and come back with the results
    – tady159
    Sep 7, 2016 at 15:17
  • 1
    Your suggestion did work. However, I had to retrieve the SharedPreferences in a different way, compared to the question you linked. getSharedPreferences method wasn't available inside the listener, so I used PreferenceManager.getDefaultSharedPreferences(getActivity()) instead. Thank you very much for the help.
    – tady159
    Sep 7, 2016 at 15:58
  • Happy to hear that, all the best. Sep 8, 2016 at 6:50
0

Like @Chisko said, there isn't enough code in your question for us to be able to figure out your end goal - although I'm guessing you are wanting some form of persistent storage for your app to be able to save your app preferences. For this, you will want to use something in Android called SharedPreferences. This allows you to save simple data types to be accessed later.

Give that link a read, and then try saving/loading one simple piece of data. You'll want to load it from SharedPreferences on starting the activity (you can specify a default value if it hasn't been saved yet) and then you'll want to save the data in onBackPressed() as you said.

Best of luck and if you run into any issues, just comment here.

1
  • Thank you for the help. @Abdulhamid Dhaiban's answer helped me solve the issue.
    – tady159
    Sep 7, 2016 at 15:59
0

@Abdulhamid Dhaiban correctly points it out.

I'll add to your suggestion about overriding the onBackPressed() method.

If your "Up" button (top left <-) provides the correct result, then you can set the Back button to behave like the Up button by just adding the following code:

@Override
public void onBackPressed() {
    super.onBackPressed();
    NavUtils.navigateUpFromSameTask(this);
}

Hope it helps!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.