java - Deprecated methods in Android Studio (AS) 1.1.0 (and Eclipse Luna) "Settings Activity" -
my app have couple of checkboxes i'd rather user didn't have re-check each time app chased memory. i've been reading preferences.
i'm not sure i've seen complicated "a modern fragment-based preferenceactivity", looking guidance/example 1.1.0, got code total of 9 references in 4 hunks of code addpreferencesfromresource
, getpreferencescreen
, , findpreference
, of carry blurb:
this method deprecated in api level 11. function not relevant modern fragment-based preferenceactivity.
to have code presented deprecated code 2 ides 1 thing; there's no indication of each method has been replaced (but there's nothing replacing it, happens).
would hard produce "a modern fragment-based preferenceactivity"? if so, guess i'm right complexity.
am missing something? supposed write/extend deprecated code? turn off compiler checking of deprecated stuff?
does know of nice tutorial (or example of) simple (like 2 checkboxes) "modern fragment-based preferenceactivity"? or saves preferences isn't deprecated?
i asked, "does know of nice tutorial (or example of) simple (like 2 checkboxes) modern fragment-based preferenceactivity
? or saves preferences isn't deprecated?"
i of 9pm last night. * edited 5/16/15 *
here complete, working preferences activity, though doesn't have drawable, mipmap, dimens, , values folders , files. downloaded advice in sams teach yourself android app development in 24 hours , modified it. emphasis mine.
androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.whatever.prefs" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="16" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.whatever.prefs.mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name="com.whatever.prefs.settingsactivity" android:label="@string/title_activity_settings" > </activity> </application> </manifest>
mainactivity.java:
package com.whatever.prefs; import android.app.activity; import android.content.intent; import android.content.sharedpreferences; import android.content.sharedpreferences.editor; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.widget.toast; public class mainactivity extends activity { public static final string settings = "com.whatever.prefs.settings"; public static final string first_use = "com.whatever.prefs.firstuse"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); /* bad idea--creates new set of prefs sharedpreferences preferences = getsharedpreferences(settings, mode_private); v v v v v v v v v v v instead! */ preferences = preferencemanager.getdefaultsharedpreferences(this); boolean firstuse = preferences.getboolean(first_use, true); if (firstuse){ toast hellomessage = toast.maketext(getapplicationcontext(), "hello first time user",toast.length_long); hellomessage.show(); editor editor = preferences.edit(); editor.putboolean(first_use, false); editor.commit(); } else { toast.maketext(getapplicationcontext(), "second+ use", toast.length_short).show(); } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.activity_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.menu_settings: intent intent = new intent(this, settingsactivity.class); startactivity(intent); return true; default: return super.onoptionsitemselected(item); } } }
settingsfragment.java:
package com.whatever.prefs; import android.content.sharedpreferences; import android.content.sharedpreferences.onsharedpreferencechangelistener; import android.os.bundle; import android.preference.preferencefragment; import android.preference.preferencemanager; import android.util.log; public class settingsfragment extends preferencefragment implements onsharedpreferencechangelistener { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.preferences); } @override public void onresume() { super.onresume(); getpreferencescreen().getsharedpreferences(). registeronsharedpreferencechangelistener(this); } @override public void onpause() { super.onpause(); getpreferencescreen().getsharedpreferences(). unregisteronsharedpreferencechangelistener(this); } @override public void onsharedpreferencechanged(sharedpreferences sharedpreferences, string key) { sharedpreferences sharedpref = preferencemanager.getdefaultsharedpreferences(getactivity()); if (key.equalsignorecase("pie_type")){ log.w("settings", sharedpref.getstring(key, "")); } } }
res\menu\activity_main.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".mainactivity"> <item android:id="@+id/menu_settings" android:orderincategory="100" app:showasaction="ifroom" android:title="@string/menu_settings"/> </menu>
res\layout\activity_main.xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerhorizontal="true" android:layout_centervertical="true" android:text="hi world" /> </relativelayout>
res\layout\activity_settings.xml:
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <fragment android:name="com.whatever.prefs.settingsfragment" android:id="@+id/settings_fragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </relativelayout>
strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">hour11app</string> <string name="hello_world">hello world!</string> <string name="menu_settings">settings</string> <string name="title_activity_settings">settings</string> <string-array name="pie_array"> <item>apple</item> <item>blueberry</item> <item>cherry</item> <item>coconut cream</item> </string-array> </resources>
styles.xml:
<resources> <!-- base application theme, dependent on api level. theme replaced appbasetheme res/values-vxx/styles.xml on newer devices. --> <style name="appbasetheme" parent="android:holo.buttonbar"> <!-- theme customizations available in newer api levels can go in res/values-vxx/styles.xml, while customizations related backward-compatibility can go here.--> </style> <!-- application theme. --> <style name="apptheme" parent="appbasetheme"> <!-- customizations not specific particular api-level can go here. --> </style> </resources>
settingsactivity.java:
package com.whatever.prefs; import android.app.activity; import android.os.bundle; public class settingsactivity extends activity { public static final string settings = "com.whatever.prefs.settings"; public static final string first_use = "com.whatever.prefs.firstuse"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_settings); } }
dimens.xml:
<resources> <!-- default screen margins, per android design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> </resources>
res\xml\preferences.xml:
<?xml version="1.0" encoding="utf-8"?> <preferencescreen xmlns:android="http://schemas.android.com/apk/res/android"> <preferencecategory android:title="images"> <checkboxpreference android:key="hires" android:title="hi-res images" android:summary="show high quality images. these take longer load" android:defaultvalue="false" /> </preferencecategory> <preferencecategory android:title="pie info"> <checkboxpreference android:key="pie" android:title="pie" android:summary="like pie" android:defaultvalue="true" /> <listpreference android:dependency="pie" android:key="pie_type" android:title="pie type" android:summary="preferred pie type eating" android:dialogtitle="type of pie" android:entries="@array/pie_array" android:entryvalues="@array/pie_array" android:defaultvalue="apple" /> <edittextpreference android:key="more_info" android:title="more info" android:summary="more pies" android:defaultvalue="" /> </preferencecategory> <preferencescreen android:key="second_preferencescreen" android:title="second screen of settings"> <edittextpreference android:key="extraa" android:title="more data" android:summary="another edittextpreference" android:defaultvalue="" /> <edittextpreference android:key="extrab" android:title="even more info" android:summary="what more can say" android:defaultvalue="" /> </preferencescreen> </preferencescreen>
Comments
Post a Comment