Change variable of another class

0

I have two activity / classes, follow the button of my first activity where when I click go to the second activity:

    public void onButtonClick(View v){
    if(v.getId() == R.id.Busuarios){
        Intent i = new Intent(Velocimetro.this,Usuarios.class);
        i.putExtra("vel",maxima);
        startActivity(i);
    }

Now inside my second activity I have the following:

        Intent i = getIntent();
    velm = i.getDoubleExtra("vel",Math.round(vel.maxima));

What I need: In this second activity I need to CHANGE the value of the MAXIMUM variable (there from the first activity).

How do I do it?

    
asked by anonymous 19.06.2016 / 00:14

1 answer

3

Two ways. The first is to set the variable max as public static and change it directly from any other class: Velocimetro.maxima = value;

Or

ActivityForResult () method Your first Activity calls the second and waits for a result.

For example:

Intent i = new Intent(Velocimetro.this, 
Usuarios.class);
startActivityForResult(i, 1);

In your second Activity, select the data you want to return to the first Activity.

For example, in the Activity Users.class:

Intent returnIntent = new Intent();
returnIntent.putExtra("resultado", resultado);
 setResult(Activity.RESULT_OK, returnIntent);
finish();

To not return data:

Intent returnIntent = new Intent();
setResult(Activity.RESULT_CANCELED, returnIntent); 
finish();

Receiving the result

Now on your first Activity write the following code in the onActicityResult () method:

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
 if (requestCode == 1) { 
    if(resultCode == Activity.RESULT_OK){
         String result = data.getStringExtra("resultado");
     }
     if (resultCode == Activity.RESULT_CANCELED) { 
//vazio
    }
 }
 }//onActivityResult
    
19.06.2016 / 12:09