1

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);
}
}
Yehuda Gutstein
  • 381
  • 10
  • 27
  • please see http://stackoverflow.com/questions/16036572/how-to-pass-values-between-fragments – Shayan Pourvatan Feb 05 '14 at 07:02
  • @Shayanpourvatan, I am having a little difficulty with that question you linked me to. Is that saying that I need to have each fragment linked to its own activity.xml in addition to being linked to the tab xml file? – Yehuda Gutstein Feb 05 '14 at 07:43
  • you need use bundle on your fragment to send and retrieve value in fragment class, – Shayan Pourvatan Feb 05 '14 at 07:50
  • I am not fully following. From inputFragment I need to send intent to new class InputActivity. From there, I send intent to new class SolutionActivity. Then I bundle intent and pass on to existing SolutionFragment. Is this correct? – Yehuda Gutstein Feb 05 '14 at 07:54
  • easiest way is create static value in FragmentActivity class and set your value to that in first fragment. – Shayan Pourvatan Feb 05 '14 at 07:56
  • I'm sorry, but it's still not clear. What is the order that the fragments and activities should flow? – Yehuda Gutstein Feb 05 '14 at 07:58

1 Answers1

0

Based on the link that was provided by Shayan Pourvatan (How to pass values between Fragments), it looks like the simple approach is to be able to:

1) Create a "bundle" with the information you want to pass. 2) From some location that has access to the instance of your fragment, call setArguments() with a bundle. 3) Read in those arguments during onCreateView() in your fragment by calling getArguments();

Really, I'm only focusing on step 5 and the part listed after that in the linked article. So because you'll be flipping between tabs, I think this approach should work well for you. Set the arguments before you switch tabs, and then when you switch to your destination tab call getArguments().

Let me know if that helps!

Community
  • 1
  • 1
Dev Leader
  • 312
  • 3
  • 9