variables - Shared Preference Method Call From Activity to Java Class -


so i'm having issue context inside sharedpreferences says loginactivity.this. device.java class , loginactivity activity want call method from. device.this or along lines?

methods:

public void validatelogin(string username, string password, string ipaddress) {      sharedpreferences sharedpreferences = preferencemanager.getdefaultsharedpreferences(loginactivity.this);      if (sharedpreferences.contains("ip") && sharedpreferences.contains("username") && sharedpreferences.contains("password")) {         string strusername = sharedpreferences.getstring("username", username);         string strpassword = sharedpreferences.getstring("password", password);         string stripaddress = sharedpreferences.getstring("ip", ipaddress);         //performlogin(strusername, strpassword, stripaddress);     } }  public void savesp(string username, string password, string ipaddress) {      sharedpreferences sharedpreferences = preferencemanager.getdefaultsharedpreferences(loginactivity.this);      sharedpreferences.edit()             .putstring("ip", ipaddress)             .putstring("username", username)             .putstring("password", password)             .commit(); } 

try this:

public class myactivity extends activity{      private static myactivity activity;      @override     protected void oncreate(bundle savedinstancestate){         super.oncreate(savedinstancestate);          activity = this;          //...     }      public static myactivity getactivity(){         return activity;     }  } 

and when need context object:

preferencemanager.getdefaultsharedpreferences(myactivity.getactivity()); 

that's usual approach when need context object outside of activity class. hope helps!


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -