1

I have an activity called ParticleActivity in my Android Studio project.

public class ParticleActivity extends AppCompatActivity  {

public final static String EXTRA_MESSAGE = "pso.algo.MESSAGE";
private ProgressDialog pd;
private double[] results = {-1.0, -1.0, -1.0};
EditText particles = (EditText) findViewById(R.id.particles);
EditText iterations = (EditText) findViewById(R.id.iterations);
static EditText solution;
public static double userSolution = Double.parseDouble(solution.getText().toString());
static EditText battery;
public static double batteryLevel = Double.parseDouble(battery.getText().toString());

The userSolution and batteryLevel are declared so that the user input in those fields can be accessed by another class, customUseCase.

public class CustomUseCase extends Test {

public ArrayList<Double> costData = MainActivity.costDATA; //costs that the user enters for each resource
public ArrayList<Double> costWlan = MainActivity.costWLAN;
public ArrayList<Double> costUtilities = MainActivity.costUTILITY;
public double batteryCost = ParticleActivity.batteryLevel; //battery cost user enters
public double userSolution = ParticleActivity.userSolution; //user's predicted solution
private int maxIter;
private int noParticles;

I know this isn't a good way of writing code as it says on Android Studio that I will get a memory leak because the Android components shouldn't be declared as static variables. But this is the only way (from my little knowledge of Android) that I can think off where I can access the user input from the EditText fields in ParticleActivity so that it can be used in my customUseCase class. Can someone give me a good way I can write this out? Thank you.

Ahmer Afzal
  • 501
  • 2
  • 11
  • 24
Az Islam
  • 133
  • 11
  • You problem seemed similar to this please check this link. [Get value from an activity to another class](http://stackoverflow.com/a/23166016/7820465) – RagnorakRage Apr 07 '17 at 15:03

1 Answers1

1

Try using Intent class and its putExtra method see the docomentation here https://developer.android.com/reference/android/content/Intent.html

example usage

Intent i = new Intent(ParticleActivity.this,CustomUseCase.class)
                     i.putExtra("myShow","one");

then on your CustomUseCase class

Intent i=getIntent();
String show=i.getStringExtra("myShow");
0xDEADBEEF
  • 590
  • 1
  • 5
  • 16