There are 2 activites where you need to fill the spinner with the same data. I fill it like this:

private void loadCategories() { final ProgressDialog pd = new ProgressDialog(this); pd.setCancelable(false); pd.show(); StringRequest strReq = new StringRequest(Request.Method.POST, AppConfig.URL_GET_CATEGORIES, new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONObject res = new JSONObject(response); JSONObject meta = res.getJSONObject("meta"); // Auth OK if (AppController.getInstance().metaCheck(meta)) { JSONObject data = res.getJSONObject("data"); JSONArray categories = data.getJSONArray("categories"); for (int i = 0; i < categories.length(); i++) { JSONObject catObj = (JSONObject) categories.get(i); Bundle b = new Bundle(); b.putString("id", catObj.getString("id_fc")); b.putString("name", catObj.getString("name")); b.putInt("is_standard", catObj.getInt("standard")); spinnerArray.add(b); } adapter.notifyDataSetChanged(); } pd.dismiss(); } catch (JSONException e) { // JSON error e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { AppController.getInstance().volleyOnErrorResponse(error); pd.dismiss(); } }) { @Override protected Map<String, String> getParams() { // Posting parameters to login url Map<String, String> params = new HashMap<>(); if (sm == null) { sm = new SessionManager(getApplicationContext()); } String accessToken = sm.getAccessToken(); params.put("access_token", accessToken); return params; } @Override public Map<String, String> getHeaders() throws AuthFailureError { //HTTP Auth return AppController.getInstance().headerAUTH(); } }; // Adding request to request queue AppController.getInstance().addToRequestQueue(strReq); } 

For each activation I copy the same code. And then for each change it is necessary to update the code in both functions. The fact is that I have a custom view for a spinner. And there are buttons like "delete category". Therefore, I don’t want to copy a huge code into each new activity.

The question is how to initialize the spinner in all activations without copying the same code?

    1 answer 1

    Make one activation class and place your method in it from the question, changing its access modifier to protected instead of private . Expand the rest of the activations from this new class, and not from the usual activations. So you will have access to this method in each activity and you will only need to change it in one place. And the number of code in your activit will be less.