Use a variable in an Activity declared in another Activity

Asked

Viewed 1,743 times

0

In my application I have a Sharedpreference variable that stores an int value for me. This value has to decrease as the user performs some actions, as if it were a counter. Let’s say that each time the user keystrokes on a specific button this value stored in Sharedpreference decreases 1 and has to be stored again with the updated value.

What happens is that I declare this Sharedpreference variable in Mainactivity which is the main code however, this screen appears only once. After the user enters a value that will be asked for it and this value is inserted in Sharedpreference the application will start running from another screen. And it is in this other screen that the value suffers its decrease as the button is clicked. And then I stopped. I do not know if I use the Intent to pass the parameter but, as I will be able to store the updated value being that this Activity will not be more open.

Is there a way to use this variable in both Ctivity? Or something like.

  • 2

    You can access any Activity at Sharedpreference, I do not understand, therefore, your question.

  • Behold: cod_final.edit().putInt("codfinal", mostrarTexto).apply(); The final variable is the one declared in the first Activity. I want her to receive another value now, but for the second Victory and still be the same value for both. When I insert this line into the second Activity the variable (cod_final) is wrong and says "cod_final cannot be resolved".

  • @kaamis, you are, shall we say, somewhat when "lost," read that, and maybe you understand.

1 answer

2


In the Mainactivity to save the value to Sharedpreference must be doing something like this;

SharedPreferences cod_final = PreferenceManager.getDefaultSharedPreferences(context);
cod_final.edit().putInt("codfinal", mostrarTexto).apply();  

In the other Activity you will need to read the recorded value use it and save it again:

SharedPreferences cod_final = PreferenceManager.getDefaultSharedPreferences(context);
//Ler o valor guardado de mostrartexto
int mostrarTexto = cod_final.getInt("codfinal", 0);
// use agora mostrarTexto como quizer, por ex. somar mais 1
mostrarTexto = mostraTexto + 1;
//grave novamente o novo valor de mostarTexto
cod_final.edit().putInt("codfinal", mostrarTexto).apply();

Back to Mainactivity use the same code to read the changed value.

Simplifying:
To record use:

SharedPreferences cod_final = PreferenceManager.getDefaultSharedPreferences(context);
cod_final.edit().putInt("codfinal", mostrarTexto).apply();

To read use:

SharedPreferences cod_final = PreferenceManager.getDefaultSharedPreferences(context);
int mostrarTexto = cod_final.getInt("codfinal", 0);
  • I tried to adapt your code and it was working but the pc I was on broke and I had to format it losing the updates because my last backup did not contain this code. In the other Activity the code is cod_final = PreferenceManager.getDefaultSharedPreferences(this); 
 int mostrarTexto2 = cod_final.getInt("codfinal",-1); to be able to read. But in Log. i do it returns the "-1" which is the default.

  • I declared the Sharedpreference variable as global before Oncreate.

  • If the default is because nothing has been recorded yet. That is to say, no cod_final.edit().putInt("codfinal", mostrarTexto).apply();

  • It had already been done in Main Activity. Anyway, I just discovered the problem: the line cod_final = PreferenceManager.getDefaultSharedPreferences(this); I think she kind of reset the variable Shared .

  • In the meantime, thank you @ramaral !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.