I have created a basic android app which uses TabHost and contains two tabs. The first tab has a fragment to input 2 values, and the 2nd tab has a fragment containing: a button to see the solution, a TextView to display the solution, and a button to reset all values in both fragments. All appears to work smoothly the 1st time I run through the app. The problem begins after viewing the solution the 1st time. If I try to enter two new numbers, and then click to see the solution, I see the last value of the TextView (either "" if having pressed reset, or the previous answer when not having pressed reset. Any suggestions as to what I may need to change would be greatly appreciated. Please let me know if any additional code is needed. Thanks in advance.
Note: All fragments import android.support.v4.app.Fragment.
InputFragment.java
public class InputFragment extends Fragment {
public static String answerString = "";
public static EditText e1;
public static EditText e2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_input,container, false);
EditText et1 = (EditText)view.findViewById(R.id.editText1);
EditText et2 = (EditText)view.findViewById(R.id.editText2);
setEt(et1, et2);
return view;
}
public void setEt(EditText a, EditText b) {
e1 = a;
e2 = b;
}
}
SolutionFragment.java:
public class SolutionFragment extends Fragment {
public String answer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_solution,container,false);
updateAnswer();
Button b1 = (Button)view.findViewById(R.id.button1);//get answer button
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setText(answer);
}
});
Button b2 = (Button)view.findViewById(R.id.button2);//reset button
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setText("");
InputFragment.e1.setText("");
InputFragment.e2.setText("");
answer = "";
}
});
return view;
}
public void updateAnswer() {
String s1 = InputFragment.e1.getText().toString();
String s2 = InputFragment.e2.getText().toString();
double num1 = Double.parseDouble(s1);
double num2 = Double.parseDouble(s2);
double ans = num1 + num2;
String ansS = String.valueOf(ans);
answer = ansS;
}
public void setText(String s) {
TextView tv = (TextView)getView().findViewById(R.id.textView2);
tv.setText(s);
}
}