如何在 FAB 按钮单击时将数据从多个片段发送到单个 activity

How to send data from Multiple Fragments to single activity on FAB button click

My problem is on clicking on b4(fab)only Fragment B data get in SecondActivity.I want Both Fragment A and Fragment B Data collected when clicking onb4Fab button available to both Fragment A and Fragment B.

Also in place of Fragment A data t4.setText(vres);,t5.setText(vres1);,t6.setText(vres2); of Second Activity get a value null.I don't understand why this is so?And always getting data from Fragment B.I want both Fragment A and Fragment B data to be fetched at SecondActivity when b4(fab)is clicked.Thanks in advance.

Fragment A

public class Fragment_nonveg 扩展片段 {

    TextView t,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
    CheckBox c1,c2,c3,c4,c5;
    Button b1,b2,b5,b6,b7,b8,b9,b10,b11,b12;
    ImageButton b3;
    int x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,g1,g2,g3,g4,g5,g6,g7,g8,g9,g10;
    String res,res1,res2,res3,res4,re,re1,re2,re3,re4;
    FloatingActionButton b4;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Bundle bundle = this.getArguments();

        return inflater.inflate(R.layout.fragment_nonveg, null);
    }
@Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        b4 = getActivity().findViewById(R.id.fab);
b4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent obj4 = new Intent(getActivity(), Bill.class);
                //obj4.putExtra("k", r1);

                int total = 0;
                String item = "", qty = "", amt = "";
                if (c1.isChecked() == true) {

                    item = item + c1.getText().toString() + "\n";
                    qty = qty + t2.getText().toString() + "\n";
                    amt = amt + t1.getText().toString() + "\n";
                    String t = t1.getText().toString();
                    total = total + Integer.parseInt(t);
                }
                if (c2.isChecked() == true) {
                    item = item + c2.getText().toString() + "\n";
                    qty = qty + t4.getText().toString() + "\n";
                    amt = amt + t3.getText().toString() + "\n";
                    String t = t3.getText().toString();
                    total = total + Integer.parseInt(t);
                }
                if (c3.isChecked() == true) {
                    item = item + c3.getText().toString() + "\n";
                    qty = qty + t6.getText().toString() + "\n";
                    amt = amt + t5.getText().toString() + "\n";
                    String t = t5.getText().toString();
                    total = total + Integer.parseInt(t);
                }
                if (c4.isChecked() == true) {
                    item = item + c4.getText().toString() + "\n";
                    qty = qty + t8.getText().toString() + "\n";
                    amt = amt + t7.getText().toString() + "\n";
                    String t = t7.getText().toString();
                    total = total + Integer.parseInt(t);

                }
                if (c5.isChecked() == true) {
                    item = item + c5.getText().toString() + "\n";
                    qty = qty + t10.getText().toString() + "\n";
                    amt = amt + t9.getText().toString() + "\n";
                    String t = t9.getText().toString();
                    total = total + Integer.parseInt(t);

                }

                int T=total;
                obj4.putExtra("key", item);
                obj4.putExtra("key2", qty);
                obj4.putExtra("key3", amt);
                obj4.putExtra("key16", T);
                getActivity().startActivity(obj4);
            }
       });

    }
}

Fragment B

public class Fragment_veg extends Fragment {


    FloatingActionButton b4;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_veg,null);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
b4 = getActivity().findViewById(R.id.fab);
b4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Do what you want
                Intent obj4 = new Intent(getActivity(), Bill.class);


                int total = 0;
                String vitem = "", vqty = "", vamt = "";
                if (vc1.isChecked() == true) {

                    vitem = vitem + vc1.getText().toString() + "\n";
                    vqty = vqty + t2.getText().toString() + "\n";
                    vamt = vamt + t1.getText().toString() + "\n";
                    String t = t1.getText().toString();
                    total = total + Integer.parseInt(t);
                }
                if (vc2.isChecked() == true) {
                    vitem = vitem + vc2.getText().toString() + "\n";
                    vqty = vqty + t4.getText().toString() + "\n";
                    vamt = vamt + t3.getText().toString() + "\n";
                    String t = t3.getText().toString();
                    total = total + Integer.parseInt(t);
                }
                if (vc3.isChecked() == true) {
                    vitem = vitem + vc3.getText().toString() + "\n";
                    vqty = vqty + t6.getText().toString() + "\n";
                    vamt = vamt + t5.getText().toString() + "\n";
                    String t = t5.getText().toString();
                    total = total + Integer.parseInt(t);
                }
                if (vc4.isChecked() == true) {
                    vitem = vitem + vc4.getText().toString() + "\n";
                    vqty = vqty + t8.getText().toString() + "\n";
                    vamt = vamt + t7.getText().toString() + "\n";
                    String t = t7.getText().toString();
                    total = total + Integer.parseInt(t);

                }
                if (vc5.isChecked() == true) {
                    vitem = vitem + vc5.getText().toString() + "\n";
                    vqty = vqty + t10.getText().toString() + "\n";
                    vamt = vamt + t9.getText().toString() + "\n";
                    String t = t9.getText().toString();
                    total = total + Integer.parseInt(t);

                }
                int vT;
                vT = total;
                obj4.putExtra("vkey", vitem);
                obj4.putExtra("vkey2", vqty);
                obj4.putExtra("vkey3", vamt);
                obj4.putExtra("vkey16", vT);
                startActivity(obj4);
            }

        });
    }

}

MainActivity

public class Menu extends AppCompatActivity {

    TextView nonveg,veg,snacks,desert,beverges;
    ViewPager viewPager;
    PageViewAdapter pageViewAdapter;
    public FloatingActionButton b4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        nonveg=(TextView)findViewById(R.id.nonveg);
        veg=(TextView)findViewById(R.id.veg);
        snacks=(TextView)findViewById(R.id.snacks);
        desert=(TextView)findViewById(R.id.desert);
        beverges=(TextView)findViewById(R.id.beverges);
        viewPager=(ViewPager)findViewById(R.id.fragment_container);
        pageViewAdapter=new PageViewAdapter(getSupportFragmentManager());
        viewPager.setAdapter(pageViewAdapter);

        nonveg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0);
            }
        });

        veg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1);
            }
        });

        snacks.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(2);
            }
        });

        desert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(3);
            }
        });

        beverges.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(4);
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onPageSelected(int position) {
                onChangeTab(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

SecondActivity

public class Bill extends AppCompatActivity {

    TextView t,t1,t2,t3,t16,t17,t4,t5,t6;
    Button b,b1;
    static   String res,res1,res2,/*res15,*/vres,vres1,vres2/*vres15*/;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill);
        t = findViewById(R.id.textView);
        t1 = findViewById(R.id.textView5);
        t2 = findViewById(R.id.textView10);
        t3 = findViewById(R.id.textView15);
        t4 = findViewById(R.id.textView6);
        t5 = findViewById(R.id.textView11);
        t6 = findViewById(R.id.textView16);
        t16 = findViewById(R.id.textView20);
        t17 = findViewById(R.id.textView21);
        b = findViewById(R.id.button22);
        b1 = findViewById(R.id.button23);
        Intent obj2 = getIntent();
        res = obj2.getStringExtra("key");
        res1 = obj2.getStringExtra("key2");
        res2 = obj2.getStringExtra("key3");
        int res15 = getIntent().getIntExtra("key16",0);
        vres = obj2.getStringExtra("vkey");
        vres1 = obj2.getStringExtra("vkey2");
        vres2 = obj2.getStringExtra("vkey3");
        int vres15 = getIntent().getIntExtra("vkey16",0);
        //vres15 = obj2.getStringExtra("vkey16");
        t1.setText(res);
        t2.setText(res1);
        t3.setText(res2);
        t4.setText(vres);
        t5.setText(vres1);
        t6.setText(vres2);
        t16.setText("Total Price:");
        int total2 = res15+vres15;
        t17.setText(String.valueOf(total2));
}
}

为了在片段和 activity 之间传递数据,您应该声明接口,然后在片段中为该接口创建一个变量,并在单击按钮时调用接口的函数之一,同时在您的 activity实现这些方法,这样你就可以通过接口函数和这些函数的参数将任何数据从片段传递到你的activity

 interface Test{
    void onSthHappend(String str);

} 
class fragment extends Fragment{
private Test interface;
 @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        interface= (Test ) context;
    }
   fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
interface.onSthHappend("hi");
            }
        });
}
clas activity Extends Activity implements Test{
@override onSthHappend(String str){
//do your stuff
}
}

In this question what is required is sharedpreferences.

Step 1:- In different Fragment of fragment container in Activity implement like this:-

public class Fragment_class_name extends Fragment {
public static int totalp;
    public static String itemp, qtyp, amtp;
    SharedPreferences sp;
    private static Fragment_nonveg instance;
@Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Bundle bundle = this.getArguments();
        instance = this;
        return inflater.inflate(R.layout.fragment_nonveg, null);
    }
public void onActivityCreated(@Nullable Bundle savedInstanceState) {

            super.onActivityCreated(savedInstanceState);
     //do your code
}
     public static Fragment_nonveg getInstance() {
            return instance;
        }
        public void yourfuncname(){//any calculative function you want to calculate or //pass data
sp=this.getActivity().getSharedPreferences("calis", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor=sp.edit();
editor.putString("key1",value1);
            editor.putString("key2",value2);
editor.commit();
        }
    }

Step 2:- Then in Your Activity do like this:-

public class Activity_class_name extends AppCompatActivity {

   
    ViewPager viewPager;
    PageViewAdapter pageViewAdapter;
    public FloatingActionButton b4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        b4 = findViewById(R.id.fab);
b4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent obj4 = new Intent(Menu.this, Bill.class);
                Fragment_class_name.getInstance().yourfuncname();
                startActivity(obj4);
            }
        });
}

Step 3:- Do this for all Fragments of fragment container