0

I have a button, part of whose functionality is in a class, that is in another class file in the same package. Is this the way how it is supposed to be done or I can somehow pass the instance of the current activity and then use whatever methods or fields it has in the methods of the second class.

public class LogInScreen extends AppCompatActivity {
    Button logInButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_in_screen);

        logInButton = findViewById(R.id.button_login);
        userText = findViewById(R.id.editText_user);
        passText = findViewById(R.id.editText_pass);
    }

    public void logIn(View view) {
        logInButton.setEnabled(false);

        String username = userText.getText().toString();
        String password = passText.getText().toString();

        BackendConnectionService.encodeCredentials(username, password);

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        BackendConnectionService.handleSSLHandshake();
        // here I put the button
        requestQueue.add(BackendConnectionService.createLogInRequest(this, logInButton));
    }
}

Declaration of the second class

public class BackendConnectionService {
    // some class declarations


    static JsonArrayRequest createLogInRequest(Context packageContext, Button button){
        final Context context = packageContext;
        final Button b = button;
        return new JsonArrayRequest(
                Request.Method.GET,
                PALLETE_URL,
                null,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Intent intent = new Intent(context, PalletsScreen.class);
                        context.startActivity(intent);
                        b.setEnabled(true);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("REST ResponseErr", error.toString());
                        Toast.makeText(context, error.toString(), Toast.LENGTH_LONG).show();
                        b.setEnabled(true);
                    }
                }) {

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<>();
                headers.put(autorisation, encodedCredentials);
                return headers;
            }
        };
    }
}
stefan.stt
  • 2,357
  • 5
  • 24
  • 47
  • This is not a good architecture, try to revise your architecture. – insa_c Jan 23 '18 at 22:26
  • @insa_c Could you give me a clue how to improve it? – stefan.stt Jan 24 '18 at 06:16
  • You shouldn't really pass any view objects to the repository layer. Instead you need to create a callback and pass that callback to the repository. Take a look at this link https://stackoverflow.com/a/48385867/5152910. Create callback interface in your repository, implement it in your activity class(this is where you need to change your button's attributes) and pass the callback object to your repository function. – insa_c Jan 24 '18 at 20:02
  • @insa_c Actually 6 hours ago I have done exactly that and it works. Thank you for your comment. – stefan.stt Jan 24 '18 at 20:08

1 Answers1

1

To be honest with you, you already did it :). You just need to know how to use it. Let's say that you want to use your LogInScreen in your createLogInRequest method:

static JsonArrayRequest createLogInRequest(Context packageContext, Button button){
    final Context context = packageContext;
    final Button b = button;

    LogInScreen logInScreen = null;
    if(context instanceof LogInScreen) {
        logInScreen = (LogInScreen) context;
    }
    //...
}
Artur Szymański
  • 1,639
  • 1
  • 19
  • 22