Android - Set variable once and use everywhere in app - java

Basically my question is I want to set lets say a string to A or B that is chosen by the user on the first screen. And then have this string variable be saved to be used on other actives else where in the app. I have see the posts and many like in Android global variable.
But the variable needs to be set and the gotten on each activity the variable isn't saved, once and then can be used everywhere?
How could this be done?
I hope i have explained this well enough, as my question differs from the one above.
The variable is not final it can be changed on the first activity but then I want to use it on the following activities, with out having to pass it with intent to each one.
Thanks for the help in advance.

You can use a public class with static String variable or passing the variable to another Activity with putExtra method.
Example :
public class Global {
public static final String DEVELOPER_KEY;
public static final String PLAYLIST_ID;
}

I have made one class for same purpose :
public class Constant {
public static int NUMBER_OF_TILES;
public static String setTilesId[] = {};
public static String TilesDesc[] = {};
public static String Image[] = {};
public static String TilesName;
public static int numberofbox = 0;
public static boolean isLogedIn = false;
public static String TilesSize = null;
public static boolean from_activity = true;
public static String URL_FOR_PRODUCT_ACTIVITY;
}
Now i can use this variables from anywhere in my APP.

You can put your variable in SharedPreferences. As it exactly matches you requirement.
You can put your variable in first screen and use it anywhere in your application. Also it is not final, you can change whenever you want it will just overwrite existing value of variable.
And MOST important thing is it will be preserved even after application is closed.
So whenever user starts your application next time you can use that variable.

A.java code:
package p2;
public class A{
public static String abc;
}
After declaring the static string in class A use it anywhere in the package using the class name as it is static so value will remain whatever you update it to.
B.java code :
package p1;
public class B{
public void methodTest(){
String s = A.abc;
}
}

Make your string public static
Ex:public static String myvar="hey"//This will be available across all classes

Instead of using static variable You can simply use Shared Preferences because after some time when your application is in background your static value become free automatically by garbage collector and become null
public void setString(Context context, String value) {
SharedPreferences prefs = context.getSharedPreferences("setString",
0);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("getString", value);
editor.commit();
}
public String getString(Context context) {
SharedPreferences prefs = context.getSharedPreferences("setString",
0);
String value = prefs.getString("getString", null);
return value;
}

You can also usw SharedPreferences:
SharedPreferences sets =getSharedPreferences(Choose_a_name, 0);
SharedPreferences.Editor editor=sets.edit();
editor.put string("from user", your string);
editor.commit();
And in your other activities you can retrieve, change and save them again. In that way, you can provide the customized data from the user over the activity lifecycle. Even If your task is completely killed, your data is available on the next launch.
To retrieve the data from another activity:
SharedPreferences sets = getSharedPreferences(yourPrefName,0);
String your string = sets.get string("from user", default value);
I want to give you alternative ideas comparing to intents or static classes. Hope it helps :)

class SharedPref
{
public void setString(Context context, String value) {
SharedPreferences prefs = context.getSharedPreferences("setString",
0);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("getString", value);
editor.commit();
}
public String getString(Context context) {
SharedPreferences prefs = context.getSharedPreferences("setString",
0);
String value = prefs.getString("getString", null);
return value;
}
}
and use this class like
SharePref pref=new SharedPref();
pref.setString("Hellooo");
String value=pref.getString();

Related

Using specific value of integer variable from TextEdit response in MainActivity, to use in a different class?

I'm attempting to code an app that takes the users numerical specifications from the main activity in the form of a TextEdit input, convert that to an integer and then use that specific value of the integer and use that value in a separate class file which I will use the result of the class in the main activity.
Is this possible? Here's what I've attempted in the global variables of the main activity:
deadzoneValue = findViewById(R.id.TextView_deadzoneInfo);
public EditText threshold, deadzone;
public String deadzoneString = deadzone.getText().toString(); //deadzone being the name of the
public int timeLimit = Integer.parseInt(deadzoneString);
public String thresholdString = threshold.getText().toString();
public static int thresholdLimit = Integer.parseInt(thresholdString);
I'm not sure how to use these in the Deadzone class, which I'm trying to take the specific value and use there.
EDIT: Deadzone.java isn't an activity but a class with functions that are called in the MainActivity.
Using of ClassicSingleton:
public class ClassicSingleton2 {
private static String instance = null;
protected ClassicSingleton2() {
}
public static String getInstance() {
return instance;
}
public static void setInstance(String instance) {
ClassicSingleton2.instance = instance;
}
}
You could change type of instance variable to int ...
And in target code you could get this data:
xRef = ClassicSingleton2.getInstance();
This is very simple.
2:
And using of put (putExtra)
Intent oI = new Intent((FirstActivity)this,SecondActivity.class);
oI.putExtra("XRefCaller",123);
And in target code(activity) :
Bundle oBundle = getIntent().getExtras();
if(oBundle != null){
oXRefCaller = oBundle.getString("XRefCaller",-1);
//checking with -1 if the parameter does not exist or is null
}

android SharedPreferences and Map

In my app I save some user information (i.e. name, weight, height) as key-value-pairs using SharedPreferences.
These information are used in several activities. So I thought, instead of implementing the whole procedure for reading/writing to the SharedPreferences in each activity, I also could implement a class "UserData" and define several static methods. So when I need some user information or want to save them I only use methods of the class "UserData" and this class handles all the stuff in background.
So I did the following things:
class UserData contains a private Map<String,?>
this map is filled by the getAll()-Method of SharedPreferences
initialization is triggerd in the onCreate()-Method in each activity
providing the values (for each possible type) to a defined key is done by the getValue(String key)-Method
writing (new) information should be done by setter methods
to write back to shared preferences, there is a save function
But now I have a lot of questions:
getAll() method
I expect, that getAll() will read all key-value-pairs from the SharedPreferences. So I would expect, that after initialization data will contain (String,String)-pairs (i.e. "name";"Max") as well as (String,Integer)-pairs (i.e. "weight",85). Am I right?
getting the values
Is the way, how I return the values in getValue(String key) correct? How can I get the value type from such a Map<String,?> or Map.Entry<String,?> definition?
adding Entries to the map
I have no idea how to overwrite or write new entries to data. One Idea was, to create a set-method for each type (i.e. String, Integer) I can save in SharedPreferences, create an Entry within this method and then calling an add-method. But how should this looks like?
saving
Will this saving fuction work properly? I'm not realy sure.
Or is this a total stupid approach?
Thanks for your support.
This is my UserData-class
public class UserData {
static private boolean isInit = false;
static private Map<String,?> data = new HashMap<>();
static void initialize(Context context){
if(UserData.isInit){
return;
}
if(context==null){
return;
}
// read data from memory
SharedPreferences pref = context.getSharedPreferences(context.getString(R.string.app_userdata),Context.MODE_PRIVATE);
UserData.data = pref.getAll();
Log.v(TAG,"loaded " + UserData.data.size() +" key-value-pairs into map");
UserData.isInit=true;
}
static void reinitialize(Context context){
UserData.isInit=false;
UserData.initialize(context);
}
static <T> T getValue(String key){
Object value = UserData.data.get(key);
if(value instanceof T){
return (T)value;
}else{
return null;
}
}
static <T> T getValue(String key,T retErr){
T value = getValue(key);
if(value!=null){
return value;
}else{
return retErr;
}
}
static void setString(String key, String str){
}
static void setInteger(String key, Integer i){
}
static private void addElement(Map.Entry<String,?> element){
}
static void save(Context context){
SharedPreferences pref = context.getSharedPreferences(context.getString(R.string.app_userdata),Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
for(Map.Entry<String,?> pair : UserData.data.entrySet()){
Object value = pair.getValue();
if(value instanceof String){
editor.putString(pair.getKey(),value.toString());
}else if(value instanceof Integer){
editor.putInt(pair.getKey(),(Integer)value);
}else if(value instanceof Float){
editor.putFloat(pair.getKey(),(Float) value);
}else if(value instanceof Boolean){
editor.putBoolean(pair.getKey(),(Boolean) value);
}
}
editor.apply();
}
}
Sample Activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UserData.initialize(getApplicationContext());
}
}
i think you are trying to recreate the wheel. if you want to create a custom class for saving, adding, or even editing shared pref, that fine. Shared pref is a map, for you to use another map, just seems backwards to me.
SharedPreferences pref = getApplicationContext().getSharedPreferences("sharedPreferences", 0); // 0 - for private mode
Storing data into Shared pref.
Editor editor = pref.edit();
editor.putBoolean("name", "bill");
editor.commit();
To get the name
String name = pref.getString("name", null); //value is null if the key 'name' doesnt exist. you can also put in any string value here
remove data from shared pref
editor.remove("name");
editor.commit();
to remove everything..
editor.clear();
editor.commit();
If you want to create a class that does this, thats fine, but dont add the data into a map.

Type safety in enum wrapper

Background info:
Sometimes you need to share a couple of global preferences in your android application and one option is to use the SharedPreferences to accomplish this;
//get the preferences
SharedPreferences prefs = myActivity().getSharedPreferences(“ConfigurationStore”, Context.MODE_PRIVATE);
//store a value
prefs.edit().putString(“user”, “Teddy”).commit();
//get the value
prefs.getString(“user”, null);
I like my code simple so I wrote a wrapper to hide the above, here is the result.
public enum ConfigurationStore {
USER(“user”);
private String key;
private SharedPreferences prefs = //get this from your activity class
ConfigurationStore(String key){
this.key = key;
}
public String get(){
return prefs.getString(key, null);
}
public void set(String value){
prefs.edit().putString(key, value).commit();
}
}
The usage of the wrapper is shown below
//Set a value:
ConfigurationStore.USER.set("Teddy");
//get a value
ConfigurationStore.USER.get()
It's easy to extend with new properties just by adding to the enum:
public enum ConfigurationStore {
USER(“user”),
DEPLOYMENT_TYPE(“deployment_type”);
....
//Set a value:
ConfigurationStore.DEPLOYMENT_TYPE.set("Beta-test");
//get a value
ConfigurationStore.DEPLOYMENT_TYPE.get()
The question:
The enum is strictly handing String properties.
Is there a way I can make this enum handle different types safely without adding other method signatures (getStringValue, getIntValue)?
I want to be able to do something like:
int age = 23;
String name = "Teddy"
ConfigurattionStore.AGE.set(age)
ConfigurattionStore.NAME.set(name)
...
age = ConfigurattionStore.AGE.get();
name = ConfigurattionStore.NAME.get();
No, not with this design.
To be able to do what you want, you would need to define a generic interface or class
public PrefHandler<T> {
T get();
void set(T);
}
And have multiple instances of this interface:
public class ConfigurationStore {
public static final PrefHandler<String> FOO = ...;
public static final PrefHandler<Integer> BAR = ...;
}

How can I retrieve Value of SharedPreference file in another Activity?

Activity 1st..
Here this is my first activity to add data
preferences=PreferenceManager.getDefaultSharedPreferences(context);
preferences = getPreferences(MODE_PRIVATE);
editor = preferences.edit();
editor.putString("userid",et_username.getText().toString());//adduserid
editor.putString("password",et_password.getText().toString());//add password
editor.commit();
Activity 2nd
This is my second activity to retrieve data.
String userName=preferences.getString("userid","");
String password=preferences.getString("password","");
Log.d("user : second", ""+preferences.getString("userid",""));
Log.d("password : second", ""+preferences.getString("password",""));
Here Log is not displayed because of null value.
Check your preferences object (probably is null). That might be the problem, as the other String variables are never null, the can be empty string ("").
Are you missing the initialization of preferences in the second Activity just in this example?
String userName=preferences.getString("userid");
String password=preferences.getString("password");
Log.d("user : second", ""+userName);
Log.d("password : second", ""+password);
Could you please try this way.
In both activities just use this to get the SharedPreferences object:
SharedPreferences prefs = getSharedPreferences("PREFS", Context.MODE_PRIVATE);
It may be that you attempt to access different preferences files from different Activities.
Or just use
PreferenceManager.getDefaultSharedPreferences(this);
i just store the one integer value you must more then one value in it..
PreferenceConnector.writeInteger(home.this, PreferenceConnector.com_id, homeComp_id);
below the preferenceConnector class to be use in it...
public class PreferenceConnector {
public static final String PREF_NAME = "Shared Preference";
public static final int MODE = Context.MODE_PRIVATE;
public static final String com_id = "com_id";
public static void writeBoolean(Context context, String key, boolean value) {
getEditor(context).putBoolean(key, value).commit();
}
public static boolean readBoolean(Context context, String key, boolean defValue) {
return getPreferences(context).getBoolean(key, defValue);
}
public static void writeInteger(Context context, String key, int value) {
getEditor(context).putInt(key, value).commit();
}
public static int readInteger(Context context, String key, int defValue) {
return getPreferences(context).getInt(key, defValue);
}
public static SharedPreferences getPreferences(Context context) {
return context.getSharedPreferences(PREF_NAME, MODE);
}
public static Editor getEditor(Context context) {
return getPreferences(context).edit();
}
}
and then u also use the share preference value to other activity like below...
int Pref = PreferenceConnector.readInteger(mainpage.this, PreferenceConnector.com_id, 0);
hope above code to be useful...

how do I access this class

import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
public class LoadSettings
{
public static void LoadMySettings (Context ctx)
{
SharedPreferences sharedPreferences = ctx.getSharedPreferences("MY_SHARED_PREF", 0);
String strSavedMem1 = sharedPreferences.getString("gSendTo", "");
String strSavedMem2 = sharedPreferences.getString("gInsertInto", "");
String cCalId = sharedPreferences.getString("gCalID", "");
setInsertIntoStr(strSavedMem2);
setSendToStr(strSavedMem1);
}
private static String cSendToStr;
private static String cInsertIntoStr;
private int cCalId;
private Uri cCalendars;
public String getSendToStr()
{
return this.cSendToStr;
}
public static void setSendToStr(String pSendToStr)
{
cSendToStr = pSendToStr;
}
public String getInsertIntoStr()
{
return this.cInsertIntoStr;
}
public static void setInsertIntoStr(String pInsertIntoStr)
{
cInsertIntoStr = pInsertIntoStr;
}
}
from the calling class i have tryed lots the current is.
LoadSettings.LoadMySettings(this);
but when i try to get some data for example.
textSavedMem1.setText(LoadSettings.getSendToStr());
i get a Null error.
LoadMySettings is not a class but a method (so it should start with a lower case, if you follow Oracle/Sun's naming conventions for the Java language).
You access it indeed by calling LoadSettings.loadMySettings(someContext), where someContext is the context to pass around. In your example, we don't know what this refers to, so maybe your error lies there.
Then when you do this: textSavedMem1.setText(LoadSettings.getSendToStr());
You call a non-static method, so that should be either using an instance of LoadSettings or, more likely considering your code, you could change getSendToStr to be:
public static String getSendToStr()
{
return cSendToStr;
}
Though that seems to be rather bad design.
Maybe if you tell us more about what you try to do, we can help more, as as such our answers will just take you one step further.
EDIT: Ok, I just figured out what you are trying to do...
You need to go back and learn basic Java concepts and read on access modifiers, and constructors first, and OO semantics in Java in general.
Change your class to this:
public class LoadSettings
{
public LoadSettings(Context ctx)
{
SharedPreferences sharedPreferences =
ctx.getSharedPreferences("MY_SHARED_PREF", 0);
String strSavedMem1 = sharedPreferences.getString("gSendTo", "");
String strSavedMem2 = sharedPreferences.getString("gInsertInto", "");
String cCalId = sharedPreferences.getString("gCalID", "");
setInsertIntoStr(strSavedMem2);
setSendToStr(strSavedMem1);
}
private String cSendToStr;
private String cInsertIntoStr;
private int cCalId;
private Uri cCalendars;
public String getSendToStr()
{
return cSendToStr;
}
public void setSendToStr(String pSendToStr)
{
cSendToStr = pSendToStr;
}
public String getInsertIntoStr()
{
return cInsertIntoStr;
}
public void setInsertIntoStr(String pInsertIntoStr)
{
cInsertIntoStr = pInsertIntoStr;
}
}
And create a new instance of LoadSettings with:
LoadSettings mySettings = new LoadSettings(someContext);
You can then correctly invoke:
textSavedMem1.setText(mySettings.getSendToStr());
Haylem has the right of it, but I just wanted to add a comment:
There are basically two design patterns in Java for what you're trying to do. One is the static class where all the methods and variables are static and you access them as e.g.
LoadSettings.loadMySettings(this);
string = LoadSettings.getSendToStr()
// etc.
The other pattern is the "singleton" class where you create exactly one instance of the class and you access the instance:
LoadSettings ls = new LoadSettings(this);
ls.loadMySettings();
string = ls.getSendToStr();
Either way is good, but what you're doing is a mish-mash of both methods and it won't work.

Categories