How can I put this out of OnCreate on Android

1

I am using global variables in the following class:

public class VariablesOpcion_HassMovil extends Application {

private String cuenta=null;

public String getCuenta(){
    return this.cuenta;
}

public void setCuenta(String Cuenta){
    this.cuenta=Cuenta;


   }

}

and in Oncreate I have the following:

VariablesOpcion_HassMovil variables = (VariablesOpcion_HassMovil) getApplication();
    variables.setCuenta("encargado");

My problem is that variable I need to use not only in OnCreate and I have tried to put it out to use it in any side of the activity but it marks me error, how could I do it?

    
asked by DoubleM 28.01.2017 в 22:48
source

1 answer

0

It is a non-static class so you can do the following, define a variable type VariablesOpcion_HassMovil at the class level:

  private  VariablesOpcion_HassMovil variables;

Before using the variable you have to instantiate it and assign the value:

 variables = new VariablesOpcion_HassMovil();
 variables.setCuenta("encargado");

With this you can get the value you assigned anywhere in your class:

Log.i("VariablesOpcion_HassMovil", variables.getCuenta());

This does not make sense, you create a variable type VariablesOpcion_HassMovil that is equal to the application but this application casteas as VariablesOpcion_HassMovil ? remember that VariablesOpcion_HassMovil extends from Application .

VariablesOpcion_HassMovil variables = (VariablesOpcion_HassMovil) getApplication();
    
answered by 30.01.2017 в 22:39