How to maintain the data of a variable, when starting an Actitivy, and when returning, load the value?

Asked

Viewed 83 times

2

I’m having the following problem, and I’d like everyone’s help please.

I have an Activity, in which, if you press btn1, it will happen Boolean mesa1 = true;

Just like for btn2... successively.

When mesa1 is true, the knob changes color, and opens a new Activity. When I do this in btn2, the knob changes color, and opens the Activity. Only when returning, btn1 is no longer the color changed, IE, mesa1 is false;

How to maintain: Boolean mesa1 = true; regardless of how many activiys I open ? Imagem inicial Ao clicar na primeira mesa, mesa1 = true, e abre a activity de pedidos A mesa ficou cinza, pois mesa1 = true Fazendo a mesma coisa, agora com a mesa2 funcionou, mas perdi o valor true da mesa1

  • The value of the variables should be kept, so it seems that when you return to the main activicty the images are reset, so you should do a check on all tables. example, create a method that does a check on all tables, if mes1 is true, then change image, and so on to all tables.

  • If the variables are not being maintained, the problem may be the scope in which you created them, or by calling the new request acitivity you may be finishing the main Activity with the Finish method. Make sure it can’t be that. Without the source code it’s very hard to know what might be going on, it’s just subjections.

  • No, there’s no Finish(), man, I just need to know how to keep the data. My problem is not a "mistake", there is no way I send the code and I have not done anything about keeping data.. that’s what I need.

1 answer

2


A solution would be.

Use the button tag attribute to save its state. xml button start tag attribute with zero android:tag="0".

Every time you change the status of the vc button you should update your tag to 1 or 0, logo btn1.setTag("1");

in the main Activity you must implement the onResume method, and check all the buttons if they are marked or not.

@Override
public void onResume(){
   super.onResume();
   if(btn1.getTag().equals("1"))
      //altera imagem
   if(btn2.getTag().equals("1"))
      //altera imagem
   if(btn3.getTag().equals("1"))
      //altera imagem
}

Now every time the main Activity is reloaded it will check the status of all buttons .

Observing: of course that’s not the best way to keep this guy information in most cases, because I believe an application like this in the future will depend on a server, which will be responsible for notify mobile phones that a table was finalized the request, this answer is valid only to answer your question, which got a too broad, as it has various ways of solving this problem.

  • Exactly, it would need a server for the application to work in a SAME trade. But as just a schoolwork, anything working is fine. Thank you so much for teaching something I didn’t know yet.

Browser other questions tagged

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