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

2

I have the following problem, and would like to help everyone please.

I have an activity, where, if you press btn1, it will happen boolean table1 = true;

As for btn2 ... in succession.

When table1 is true, the button changes color, and opens a new activity. When I do this in btn2, the button changes color, and opens the activity. But when returning, btn1 is no longer with the color changed, ie, table1 is false;

How to keep: boolean table1 = true; regardless of how many activiys I open?

    
asked by anonymous 28.11.2015 / 20:57

1 answer

2

One solution would be.

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

Whenever you change the status of the button you should update your tag to 1 or 0, so btn1.setTag("1");

In the main activity you must implement the onResume method, and check all the button if it is checked 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 states of all the buttons.

  

Note: Of course this is not the best way to save this type   of information in most cases, as I believe an application like that in the future   will depend on a server, which will be responsible for   notify the cell phones that a table has been finalized the request, this   The answer is valid only to answer your question, which   a little too wide, because it has several ways to solve this problem.

    
29.11.2015 / 04:39