For example:
int a=5; //in MainActivity.java
int b=7 //in MainActivity2.java
int c=8 //in MainActivity3.java
and these three variables in another Activity i.e.
int total= a+b+c //in MainActivity4.java
For example:
int a=5; //in MainActivity.java
int b=7 //in MainActivity2.java
int c=8 //in MainActivity3.java
and these three variables in another Activity i.e.
int total= a+b+c //in MainActivity4.java
way 1: you can use public static variable.but static variable is memory leak.
public static int a=5; //in MainActivity.java
public static int b=7 //in MainActivity2.java
public static int c=8 //in MainActivity3.java
int total= a+b+c //in MainActivity4.java
way 2: other way, save a,b,c in shared preference and in MainActivity4.java load there and use.
You can just make those variables static
, and can access them from MainActivity4
. If you want to make these variables private
and write protected, then use a static getter method.
As mentioned in your code you are using
int a=5; //in MainActivity.java
int b=7 //in MainActivity2.java
int c=8 //in MainActivity3.java
To start activity you must be using below code
Intent intent = new Intent(this, yourRequiredActivity.class)
intent .putExtra("firstVariable", a);
intent .putExtra("secondVariable", b);
intent .putExtra("thirdVariable", c);
startActivity(myIntent);
And when new Activity starts use below code
Intent mIntent = getIntent();
int a= mIntent.getIntExtra("firstVariable", 0);
int b= mIntent.getIntExtra("secondVariable", 0);
int c= mIntent.getIntExtra("thirdVariable", 0);
And now you can use below code for your operation
int total= a+b+c
hope that answers your question.