java - Can't access SharedPreferences in class that extend Application -
i'm trying code global method extending application
class. i'm trying add 2 method rapidly access 1 value stored in sharedpreferences
. unfortunately can't make sharedpreferences
work, here code:
package postfixcalculator.mattiarubini.com.postfixcalculator; import android.app.application; import android.content.context; import android.content.sharedpreferences; public class postfixcalculatorextendapplication extends application { public void changepreference (boolean flag){ //i'm updating preference file based on purchase sharedpreferences sharedpref = getactivity().getpreferences(context.mode_private); sharedpreferences.editor editor = sharedpref.edit(); editor.putboolean(getstring(r.string.purchase_status), flag); editor.commit(); } public boolean retrievepreferences (){ sharedpreferences sharedpref = this.getpreferences(context.mode_private); /*if file doesn't exist, create file , i'm returned false value. * because if file not created it's first install. * if user acquired product on store, able restore purchase.*/ boolean flagpurchase = sharedpref.getboolean(getstring(r.string.purchase_status), false); return flagpurchase; } }
i understand getactivity()
doesn't work in activity
in fragment
, don't know in specific case. in activity
, make sharedpreferences
works, need use key word this
.
these errors get:
cannot resolve method 'getactivity()'
cannot resolve method 'getpreferences(int)'
try getapplicationcontext() getsharedpreferences()
Comments
Post a Comment