So I followed this guide on Android Developers. They suggest to use fragments for showing settings to user.
I created the xml and strings and the fragment:
public class SettingsFragmentApp extends PreferenceFragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences_app);
}
}
I want to show this on my MainActivity page without creating another activity that only hosts this fragment (I think the later optioon recommended by google kills the point...why should I create another activity for just a fragment?). So I added an option to the MENU
and I handle it like this in the MainActivity
:
//inside onOptionsItemSelected(MenuItem item)
case (R.id.action_settings_user):
getFragmentManager().beginTransaction().replace(android.R.id.content,
new SettingsFragmentUser()).commit();
return true;
This way the setting fragments shows up as expected, but as soon as user hits back button the application exits because it was still on MainActivity
.
So the question is how can I handle the back button in such a way that it saves the settings changes and brings back user to the MainActivity?