New to Android app programming. Learning by doing but some things seem bizarrely opaque. Have a simple app going with a MainActivity
and menu with a Settings option.
I want to be able to set my version numbers in one place (the app build.gradle
file). I manually change the version name and the build number (versionCode) perpetually increments.
def Properties versionProps = new Properties()
def versionPropsFile = file('version.properties')
if (versionPropsFile.exists())
versionProps.load(new FileInputStream(versionPropsFile))
def buildNum = (versionProps['BUILD_number'] ?: "0").toInteger() + 1
versionProps['BUILD_number'] = buildNum.toString()
versionProps.store(versionPropsFile.newWriter(), null)
defaultConfig {
applicationId "com.someCorp.someApp"
minSdkVersion 22
targetSdkVersion 25
versionCode buildNum
versionName "0.2.7"
}
In my app, I get the versionCode
and versionName
and I break the latter down to Major, Minor, and Release in my Version class.
PackageInfo packageInfo = getPackageManager ().getPackageInfo (getPackageName (), 0);
int versionBuild = packageInfo.versionCode;
String versionName = packageInfo.versionName;
myVersion = new Version (versionBuild, versionName);
myversion.toString ()
produces
someApp Version 0.2.7.267\nMajor Version 0, Minor Version 2, Release 7, Build 267.
I do this so that even naive users will be able to tell me exactly which version/release they are using when I will get the inevitable support calls.
I can then use defaultSharedPreferences
to save this String and retrieve it.
However, when I use the recommended way of doing a Settings / Preferences menu item launching a subclass of PreferenceActivity
using the modern PreferenceFragment
way as follows ...
public boolean onOptionsItemSelected (MenuItem item) {
switch (item.getItemId ()) {
case R.id.menu_settings_item:
Intent i = new Intent (this, someAppPreferenceActivity.class);
startActivityForResult (i, RESULT_SETTINGS);
break;
.
public class someAppPreferenceActivity extends PreferenceActivity {
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
getFragmentManager ().beginTransaction ().replace (android.R.id.content, new MyPreferenceFragment ()).commit ();
} // OnCreate (Bundle)
public static class MyPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate (final Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
addPreferencesFromResource (R.xml.settings);
} // onCreate (Bundle)
} // class MyPreferenceFragment
... the Settings view displays the setting from my "settings.xml
" resource file (naturally):
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="pref_version"
android:title="@string/pref_version"
android:summary="@string/pref_version_summ"
android:defaultValue="true" />
</PreferenceScreen>
and strings.xm
l has
<string name="pref_version">someApp Version 0.1.1.18\n Major version 0\n Minor version 1\n Release 1\n Build 18</string>
which is a bogus string meant to be overwritten with the actual values.
My Question:
How do I get the changes I save in the SharedPreferences
(hidden) file (wherever that is) to appear in my settings.xml
so that they appear in the Settings view? I could skip the SharedPreferences
step if I could save directly to settings.xml
or to the Settings view.
I have done much searching but there seems to be no way to connect the two. Examples and tutorials explain both ways of doing things but not how to connect them (get saved preferences to appear in the Settings menu).
http://www.101apps.co.za/index.php/articles/preference-settings.html
http: //viralpatel.net/blogs/android-preferences-activity-example/
https: //developer.android.com/reference/android/preference/PreferenceActivity.html
I can't find methods to access the PreferencesFromResource
to change the value before it gets added into the Settings view. Theoretically I should be able to step through the fragments and change one and have it appear in the view, but I can't find methods to step through them. SharedPreferences
has a workable key-value pair system that is nice.
I can't find anything like PreferenceFragment.setValue (String key, String value)
or edit()
.
For example, this question How to get a preference value in preference fragment has an answer that gets SharedPreferences
inside the Fragment, but doesn't really answer the question as to how to hook them up in conjunction with the Settings view. I can read and see the saved SharedPreferences
value in there, but how to stick it into the hierarchy of Fragments or add it to the Settings view?