In the process of learning Java and Android. I understand that I am trying to do but can't seem to make it work. I created a global class as seen here
In another class I have a method where I set the value of the status.
At the entry of the activity I set the global using
From what I can tell the g.setStatus(status) works from the point that I don't receive any errors. After setting the variable, I goMain(), which for me is just a second activity with a text box where I attempt to call the global back and display in the text box. I am calling the global as such
Before somebody says use intent and putextras, yes I could and know how but I want to get global variables to work if for nothing more than my knowledge of knowing how to correctly. I have tested the text box using put and get extra so I know it works, but trying to set and get the global variables it does not. Can somebody point out the mistake I am making?
Thanks
Code:
public class Globals {
private static Globals instance;
private String status;
public Globals() {
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public static Globals getInstance() {
if (instance == null) instance = new Globals();
return instance;
}
}
In another class I have a method where I set the value of the status.
At the entry of the activity I set the global using
Code:
Globals g= Globals.getInstance()
[code]public void getStatus() {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
serverURL, new Response.Listener<JSONObject>() {
if (response == null) {
} else {
try {
String status = (response.getString("status"));
if (Integer.parseInt(status) != 0) {
g.setStatus(status);
goMain();
} else {
// TODO: 10/24/2017 IF ZERO GO TO ACTIVATION ERROR ACTIVITY
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(jsonObjectRequest);
}
From what I can tell the g.setStatus(status) works from the point that I don't receive any errors. After setting the variable, I goMain(), which for me is just a second activity with a text box where I attempt to call the global back and display in the text box. I am calling the global as such
Code:
public class HomeActivity extends Activity {
Globals g= Globals.getInstance();
private TextView mytext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mytext=(TextView) findViewById(R.id.textView);
String test= g.getStatus();
mytext.setText(test);
}
}
Before somebody says use intent and putextras, yes I could and know how but I want to get global variables to work if for nothing more than my knowledge of knowing how to correctly. I have tested the text box using put and get extra so I know it works, but trying to set and get the global variables it does not. Can somebody point out the mistake I am making?
Thanks
I'm a purist, so that kind of thing really grates on me.