如何从主类向另一个Activity调用方法

时间:2019-02-26 21:04:20

标签: java android class methods

我尝试了这种方法: MainActivity ma = new MainAcivity(),ObtenerModuloActivado, 但这不起作用...

我的MainActivity中有此方法:

private void obtenerModulosActivos() {
        ConectWS conectWS = new ConectWS(this, new OnTaskCompleted() {
            @Override
            public void onTaskCompleted(Object feed) {
                setMenuOptions(true);
            }

            @Override
            public void onTaskError(Object feed) {
                Toast.makeText(getApplicationContext(), "Error en metodo ObtenerModulosA()", Toast.LENGTH_LONG).show();
            }
        });

        if (oCnn.estaConectado()) {
            mostrarUOcultarLoader(true);
            DatosEvento datosEvento = DatosEvento.obtenerInstancia(getApplicationContext());
            conectWS.execute("ModulosActivos", datosEvento.obtenerIdEvento());
        }
    }

并希望将其称为另一个活动:

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case android.R.id.home:
                onBackPressed();
///   ----->HERE I WANT TO DECLARE THE METHOD ObtenerModulosActivos();
                ///se agrego esto por rafa
                AgendaGActivity.this.finish();

                break;
            case R.id.menu_busqueda:
                abrirBusqueda();
                break;
        }

        return true;
    }

1 个答案:

答案 0 :(得分:1)

您必须将其定义为public static

class CommentsExample extends Component {
  state = {
    text: '',
  }

  handleSubmit = () => {
    this.props.postComment(this.state.text);
  }

  render() {
    return (
      <View>
        <TextInput
          style={{height: 40}}
          placeholder="Type your Comment here"
          onChangeText={(text) => this.setState({text})}
        />
        <Button
          title="Submit"
          onPress={this.handleSubmit}
        />
      </View>
    );
  }
}

以及其他活动

public static void obtenerModulosActivos()