Tuesday 6 August 2013

How to call a button onClick Method from another activity

How to call a button onClick Method from another activity

I have two Activities named Main and Results. In the main I have a START
button that sends user entered data to a Bluetooth device, reads back
measurement data from the BT device, processes the data and then graphs it
in the Results activity. In the Results activity I wish to have another
START button that performs that exact same function so the user does not
have to keep switching screens to take another measurement. I made the
START button in the Main activity static so I can call if from the Results
activity. This seems to work, but it keeps opening more Results activities
every time the START button is pressed again. I tried to fix this by
calling finish() each time the START button in the Results activity is
pressed. This works, but causes the screen to flip back to the main
activity briefly and then back to the Results activity once all the data
is collected again. I would like to just stay on the Results activity and
simply see the graphs change.
1.) I don't believe its good practice to have the static
functions/variables. Is there a better way to call this START method from
another activity?
2.) Any suggestions on how to close the Results activity to avoid
duplicates but keep the screen from changing back and forth quickly
between the activities.
START method from Main Activity:
public static void start_sweep(View view) {
try{
System.out.println("in start_sweep");
What_Button = 0;
start_freq =
Double.parseDouble(start_freq_input.getText().toString());
stop_freq =
Double.parseDouble(stop_freq_input.getText().toString());;
step_size =
Double.parseDouble(step_size_input.getText().toString());;
System.out.println(start_freq + " " + stop_freq + " " + step_size);
short steps = (short) Math.round((stop_freq-start_freq)/step_size);
_steps = (short) (steps + 1);
gain = new double[_steps];
phase = new double[_steps];
int FTW_step = (int) (Math.round(step_size/CLK_freq*Math.pow(2,
32)));
int FTW_start = (int) (Math.round(start_freq/CLK_freq*Math.pow(2,
32)));
WriterThread wt = new WriterThread(mConnectThread.mmOutStream,
steps, FTW_step, FTW_start);
wt.start();
start.setEnabled(false);
NextBtn.setEnabled(true);
NextBtn.setImageResource(R.drawable.next_enabled);
//Display state in "status" textview
output.setText("Collecting Data...");
}catch (Exception e){
e.printStackTrace();
output.setText("Please Enter Sweep Parameters");
}
}
Method called in Main Activity to start Results Actiivity:
public void graphData() {
//Creat X-axis array then pass it to Results and interleave with gain
and phase
X_axis = new double[_steps];
X_axis[0] = start_freq;
for (int i=1; i<_steps; i++) {
X_axis[i] = X_axis[i-1] + step_size;
}
//start Results activity after button is pressed to display graph
Intent intent = new Intent(this, Results.class);
intent.putExtra("gainData", gain);
intent.putExtra("phaseData", phase);
intent.putExtra("Xaxis", X_axis);
intent.putExtra("Battery Voltage", BatVolt);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
start.setEnabled(true);
}
START method from Results activity:
public void start_sweep2 (View view) {
MainActivity.start_sweep(view);
finish();
}

No comments:

Post a Comment