I have this method that I have in every activity (which just works) but can I make a 1 activity like API and call this function from any activity? and would this be a good solution? Instead of having the same function in every activity?
I have this function in a clean activity and want to call this function from any activity. In these lines I get the error: cannot be referenced from a static context
final String email = SharedPrefManager.getInstance(this).getUserEmail();
final TextView bottom_bar_points = findViewById(R.id.bottom_bar_points);
...
MySingleton.getmInstance(this).addToRequestQueue(request);
I would love to see some advice, thanks in advance.
Code
public static void getMyPoints() {
final String email = SharedPrefManager.getInstance(this).getUserEmail();
final TextView bottom_bar_points = findViewById(R.id.bottom_bar_points);
String uRl = "https://mywebsite.com/getmypoints.php";
StringRequest request = new StringRequest(Request.Method.POST, uRl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.equals("Found")) {
bottom_bar_points.setText(response);
return;
} else {
bottom_bar_points.setText(response);
return;
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
bottom_bar_points.setError(error.toString());
return;
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> param = new HashMap<>();
param.put("email", email);
return param;
}
};
request.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
MySingleton.getmInstance(this).addToRequestQueue(request);
}