0

I have a tabbed activity that have two fragments the fragment1 has edit text fields and a button; the second fragment has edit text fields, i want when i press the button it takes the numbers in the edit text in fragment1 and do some calculation then show the result in fragment2 edit text. Im stuck at show the result in the second fragment here is the first fragment

public  class PlaceholderFragment extends Fragment {

private static final String ARG_SECTION_NUMBER = "section_number";

public PlaceholderFragment() {
}

 public static PlaceholderFragment newInstance(int sectionNumber) {
    PlaceholderFragment fragment = new PlaceholderFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_SECTION_NUMBER, sectionNumber);
    fragment.setArguments(args);
    return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View rootView = inflater.inflate(R.layout.fragment_activity_beam_rec, container, false);

    final EditText etxb,etxh,etxd;
    final EditText etxt1,etxt2,etxt3;

    final Button buDesign = (Button)rootView.findViewById(R.id.buDesign);

    etxb = (EditText)rootView.findViewById(R.id.editText);
    etxh = (EditText)rootView.findViewById(R.id.editText2);
    etxd = (EditText)rootView.findViewById(R.id.editText3);

    buDesign.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            double b,d,h,asst;

                b = Double.valueOf(etxb.getText().toString());
                d = Double.valueOf(etxd.getText().toString());
                h = Double.valueOf(etxh.getText().toString());



                   asst = fn.asst(h,b,d);
var.asttt = asst;
                ActivityBeamRec.mViewPager.setPagingEnabled(true);
                ActivityBeamRec.mViewPager.setCurrentItem(2);
            }
    });
        return rootView;
    }
}

and here is the second fragment

    public class FragBeamRec extends Fragment {
        public static FragBeamRec newInstance() {
            FragBeamRec fragment = new FragBeamRec();
            return fragment;
              }

            public FragBeamRec() {
            }
            @Override
            public View onCreateView(  LayoutInflater inflater, ViewGroup container,
                                     Bundle savedInstanceState) {
                 View rootView = inflater.inflate(R.layout.fragment_frag_beam_rec, container, false);
             TextView tass = (TextView)rootView.findViewById(R.id.txttttaasss);
             tass.setText(String.valueOf(var.asttt));
               return inflater.inflate(R.layout.fragment_frag_beam_rec, container, false);
        }

     //@Override
     //   public void onResume() {
     //  super.onResume();
     // if (var.fuckjava) {
     //      var.fuckjava=false;
     //     FragmentTransaction ft = getFragmentManager().beginTransaction();
     //      ft.detach(this).attach(this).commit();
     // }
    }
    }

as you can see i tried to put the result in a global variable and then use it in fragment2 but it doesn't work, i think it because it is in the on create view stage so i but a code to refresh the fragment and it doesn't work. then i tried to make it show the result in the edit text in the on resumed stage but i cant because the edit text isn't readable in the on resumed class. what can i do. please help I'm new in java

tamim abweini
  • 459
  • 6
  • 21
  • You can use SharedPreferences or store the variable on the parent Activity of both of the Fragments. – chRyNaN Mar 21 '16 at 19:02

1 Answers1

1

step 1.to send data from fragment to activity

Intent intent = new Intent(getActivity().getBaseContext(),
                        TargetActivity.class);
                intent.putExtra("message", message);
                getActivity().startActivity(intent);

step 2.to receive this data in Activity:

Intent intent = getIntent();
String message = intent.getStringExtra("message");

step 3. to send data from activity to another activity follow normal approach

Intent intent = new Intent(MainActivity.this,
                        TargetActivity.class);
                intent.putExtra("message", message);
                startActivity(intent);

step 4 to receive this data in activity

    Intent intent = getIntent();
  String message = intent.getStringExtra("message");

Step 5. From Activity you can send data to Fragment with intent as:

Bundle bundle=new Bundle();
bundle.putString("message", "From Activity");
  //set Fragmentclass Arguments
Fragmentclass fragobj=new Fragmentclass();
fragobj.setArguments(bundle);
and to receive in fragment in Fragment onCreateView method:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
          String strtext=getArguments().getString("message");

    return inflater.inflate(R.layout.fragment, container, false);
    }

Source: How to pass values between Fragments

Community
  • 1
  • 1
Automatik
  • 329
  • 1
  • 7
  • 15