Is it possible to invoke a super class method from the onClick eventon?

0

It turns out that I have a button that performs an action with an if but it turns out that if it does not enter that if I need to run a method of an android class that are the ones that put the menu in the action bar:

btnBuscar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (TextUtils.isEmpty(primerNombre.getText().toString().trim())){
                customToadError(getApplicationContext(),"Debe ingresar al menos un criterio de búsqueda");
            }else{
             // aqui deberian ejecutarse los metodos de sobreescritos que pondrian una opcion

            }
        }
    });

these are the methods about writing:

 public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.add, menu);
    return super.onCreateOptionsMenu(menu);
}

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_agregar) {
       Intent i=new Intent(ListSrActivity.this, NuevoSRActivity.class);
        startActivity(i);
    }
    return super.onOptionsItemSelected(item);
}
    
asked by Igmer Rodriguez 14.10.2018 в 00:53
source

1 answer

0

The onCreateOptionsMenu(Menu menu) method is called by Android directly to build the toolbar or action bar menu.

As for the second method, instead of trying to execute the action by emulating the button, directly invoke the action to execute:

For example:

btnBuscar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (TextUtils.isEmpty(primerNombre.getText().toString().trim())){
                customToadError(getApplicationContext(),"Debe ingresar al menos un criterio de búsqueda");
            }else{
             // aqui deberian ejecutarse los metodos de sobreescritos que pondrian una opcion
                iniciarNuevoSRActivity();
            }
        }
    });


public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_agregar) {
       iniciarNuevoSRActivity();
    }
    return super.onOptionsItemSelected(item);
}

private void iniciarNuevoSRActivity(){
    Intent i=new Intent(ListSrActivity.this, NuevoSRActivity.class);
    startActivity(i);
}
    
answered by 14.10.2018 в 03:01