android - Short preferences activity (two options) not retaining values if app is removed from memory. -


here's attempt @ preferences settings activity. it's hacked, has no deprecated methods. grew out of hack did not use preferences. hack works until app leaves list of apps in memory, @ point values reset if firstuse upon next use.

adding preferences code (in dealwithpreferences , onoptionsitemselected) changed nothing that. works fine long app remains in memory. once leaves, we're firstuse.

the added code came modifying textbook example, not "modern" way of dealing preferences. short. it's sams teach android app development in 24 hours. guess i'm not challenge. or maybe code in text buggy. or both.

i want save persistently 2 preferences. far, no go.

here's 94-line sscce xml follow. hacked off ton of code, leaving barest of bones see problem. (btw, tried extends preferenceactivity; no change.)

    package com.whatever.prefer;     import android.app.activity;     import android.content.sharedpreferences;     import android.os.bundle;     import android.view.menu;     import android.view.menuitem;     import android.view.viewconfiguration;     import android.view.window;     import java.lang.reflect.field;      public class myactivity extends activity{       boolean debugging = false, firstuse, screensaver, focusatclue;       public final string           prefix = "com.whatever.prefer",           settings = prefix + ".settings",           first_use = prefix + ".firstuse",           focus_at_clue = prefix + ".focusatclue",           screensaver = prefix + ".screensaver",           literally_focus_at_clue = "focus @ clue",           literally_screen_saver = "screen saver";       sharedpreferences preferences;       sharedpreferences.editor editor;        private void dealwithpreferences(){         preferences = getsharedpreferences(settings, mode_private);         editor = preferences.edit();         boolean firstuse = preferences.getboolean(first_use, true);         if (firstuse) {           editor.putboolean(first_use, false);           focusatclue = true;           screensaver = true;           editor.putboolean(focus_at_clue, focusatclue);           editor.putboolean(screensaver, screensaver);         }         else {           editor.putboolean(first_use, false);           editor.putboolean(focus_at_clue, focusatclue);           editor.putboolean(screensaver, screensaver);         }         editor.commit();       }        @override protected void oncreate(bundle savedinstancestate)  {         dealwithpreferences();         super.oncreate(savedinstancestate);         getwindow().setformat(window.feature_action_bar);         makeactionoverflowmenushown();       } // oncreate        private void makeactionoverflowmenushown(){ // hack         try {           viewconfiguration config = viewconfiguration.get(this);           field menukeyfield = viewconfiguration.class.getdeclaredfield("shaspermanentmenukey");           if (menukeyfield != null) {             menukeyfield.setaccessible(true);             menukeyfield.setboolean(config, false);           }         } catch (exception e) {         }       }        @override public boolean oncreateoptionsmenu(menu menu) {         getmenuinflater().inflate(r.menu.menu_my, menu);         return true;       }        @override public boolean onoptionsitemselected(menuitem item) {         preferences = getsharedpreferences(settings, mode_private);     // toggle whichever item tapped         switch (item.getitemid()) {           case r.id.itemscreensaver:             if (item.gettitle().tostring().equals(literally_screen_saver)) {               item.settitle("no " + literally_screen_saver);               screensaver = false;             }             else {               item.settitle(literally_screen_saver);               screensaver = true;             }             break;           case r.id.itemfocus:             if (item.gettitle().tostring().equals(literally_focus_at_clue)) {               item.settitle("no " + literally_focus_at_clue);               focusatclue = false;             }             else {               item.settitle(literally_focus_at_clue);               focusatclue = true;             }         } // switch         dealwithpreferences();         return true;       }     } 

here's menu_my.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=".myactivity">      <item android:id="@+id/itemhelp"           android:title="tap below toggle option..."           android:orderincategory="10"           app:showasaction="never"/>      <item android:id="@+id/itemfocus"           android:title="@string/focusatclue"           android:orderincategory="200"           app:showasaction="never"/>      <item android:id="@+id/itemscreensaver"           android:title="@string/screensaver"           android:orderincategory="300"           app:showasaction="never"/>  </menu> 

and androidmanifest.xml.

<?xml version="1.0" encoding="utf-8"  ?>     <manifest         xmlns:android=  "http://schemas.android.com/apk/res/android"         package=        "com.whatever.prefer"         >          <application             android:icon="@mipmap/ic_launcher"             android:allowbackup="true"             >              <activity                 android:screenorientation=  "portrait"                 android:label=              "bs"                 android:icon=               "@mipmap/ic_launcher"                 android:name=               ".myactivity"                 >                  <intent-filter>                      <action                         android:name=   "android.intent.action.main"                     />                      <category                         android:name=   "android.intent.category.launcher"                     />                  </intent-filter>              </activity>          </application>      </manifest> 

what supposed happen this. when user taps action bar overflow settings icon, menu below pops , user can toggle either "option", either add "no" text of option (if absent) or take away "no" (if present).

it works doesn't persist.

i realize hacked, it's need. surely there's easy fix (i hope).

what should fix it?

enter image description here


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -