Y to X
Y to X

Reputation: 41

How to use a variable from one activity to another activity

I want to use a variable(answer) that I got from my code by calculating 3 numbers together on other activity, I want to calculate a new variable using the answer I got from the previous calculation on activity one in activity 2 . I tried to search on Internet but there isn't much about it , or i'ts all about passing a text value to another page and by displacing it . I somehow manage to get this idea right at the first time , but my emulator won't work.

this is my code , I hope I made it sensible .

Activity 1

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_male);

    age =(TextView)findViewById(R.id.age);
    height=(TextView)findViewById(R.id.heigth);
    weight =(TextView)findViewById(R.id.weigth);
    result =(TextView)findViewById(R.id.result);

    eage=(EditText)findViewById(R.id.eage);
    eheight=(EditText)findViewById(R.id.eheight);
    eweight=(EditText)findViewById(R.id.eweight);


    calculate=(Button)findViewById(R.id.calculate);
    back =(Button)findViewById(R.id.back);
    next =(Button)findViewById(R.id.next);

    calculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            double n1=Double.parseDouble(eage.getText().toString());
            double n2=Double.parseDouble(eheight.getText().toString());
            double n3=Double.parseDouble(eweight.getText().toString());
            double ans=(90+4.8*n2+13.4*n3-5.7*n1);
            result.setText("Your BMR is "+ans);



        }
    });

    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i=new Intent(MALE.this,MainActivity.class);
            startActivity(i);
        }
    });

    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(MALE.this, MALERUN.class);
            double n1=Double.parseDouble(eage.getText().toString());
            double n2=Double.parseDouble(eheight.getText().toString());
            double n3=Double.parseDouble(eweight.getText().toString());
            double ans=(90+4.8*n2+13.4*n3-5.7*n1);
            result.setText("Your BMR is "+ans);
            i.putExtra("answer",ans);
            startActivity(i);
        }
    });

}

Activity 2

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_malerun);
    calculate=(Button)findViewById(R.id.calculate);
    back =(Button)findViewById(R.id.back);
    next =(Button)findViewById(R.id.next);
    result =(TextView)findViewById(R.id.result);
    time=(EditText)findViewById(R.id.time);


    calculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (time.getText().equals(0));
            Intent i = getIntent();
            double[] n1=i.getDoubleArrayExtra("answer");
            double ans = (n1*1.2);



        }
    });







}

Upvotes: 1

Views: 65

Answers (2)

Mehrdad
Mehrdad

Reputation: 1637

best way:

in activity 1:

public static String myVar;

in activity 2:

String m=activity1.myVar;

Upvotes: 2

Chirag Savsani
Chirag Savsani

Reputation: 6140

You can pass data using Intent

Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("ANSWER", answer);
startActivity(intent);

And in Activity 2.class on Create

double answer = getintent().getDoubleExtra("ANSWER", 0.0);

Upvotes: 2

Related Questions