Last active
August 29, 2015 14:09
-
-
Save robertoestivill/99133bd65144f987cd64 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Podes llamar al PreferenceHelper desde cualquier lado de codigo, aun sin un contexto. | |
* | |
*/ | |
public MyFragment extends Fragment{ | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | |
... | |
PreferenceHelper.save("MY_KEY", "MY_VALUE"); | |
MyPojo myPojo = new MyPojo(); | |
Log.d("DEBUG", myPojo.name ); | |
} | |
class MyPojo{ | |
String name = PreferenceHelper.getString("name"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Para utilizar el mismo archivo de preferences desde tu PreferenceActivity, tenes que especificarle el nombre del mismo. | |
* De esta manera, todas las preferences que declares en el xml activity_preferences van a ser persistidas en el mismo archivo | |
* que las preferencias manejadas por PreferenceHelper. | |
*/ | |
public class MyPreferencesActivity extends PreferenceActivity{ | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
getPreferenceManager().setSharedPreferencesName("MyPreferencesFileName"); | |
addPreferencesFromResource(R.xml.activity_preferences); | |
... | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* En el Application (primer codigo de la app que se ejecuta siempre), se inicializa el PreferenceHelper. | |
*/ | |
public class MySuperApplication extends Application { | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
PreferenceHelper.init(this, "MyPreferencesFileName"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Hay dos opciones para utilizar esta clase | |
* 1) Wrappear staticamente los metodos de SharedPreferences, como esta en este ejemplo. | |
* 2) Exponer el objeto SharedPreferences a traves de un singleton | |
*/ | |
public final class PreferenceHelper { | |
private static SharedPreferences mSharedPreferences; | |
public PreferenceHelper() { | |
} | |
public static void init(Context context, String fileName) { | |
mSharedPreferences = context.getSharedPreferences(fileName, Context.MODE_APPEND); | |
} | |
public static boolean hasPreference(String key) { | |
return mSharedPreferences.contains(key); | |
} | |
public static void save(String name, String value) { | |
mSharedPreferences | |
.edit() | |
.putString(name, value) | |
.apply(); | |
} | |
public static void save(String name, int value) { | |
mSharedPreferences | |
.edit() | |
.putInt(name, value) | |
.apply(); | |
} | |
public static String getInt(String name) { | |
return mSharedPreferences.getString(name, null); | |
} | |
public static int getInt(String name) { | |
return mSharedPreferences.getInt(name, 0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment