简体   繁体   中英

Communication between Activity and Fragment for sending values

Hi guys then in the program you see below I should perform this series of steps:

1) Fragment1 (Rapportini) I go to Activity2 (Articoli)

2) select an element and return to Fragment1 (Rapportini) passing the value as intent

3) the OnActivityResult method reads the intent and adds it to an arraylist

My problem is that the OnActivityResult method is not called! Why this happens, how can I solve it?

Activity 2 (Articoli) - Code:

  Articolo a = (Articolo) ArticoliRicerca.get(position);
                Intent tabRapportini = new Intent(ArticoliActivity.this, RapportiniActivity.class);
                tabRapportini.putExtra("articoloselezionato", a);
                setResult(ArticoliActivity.RESULT_OK,tabRapportini);
                System.out.println("\n  Hai fatto tap  \n");
                finish();

Fragment result (Rapportini Fragment ):

public class RapportiniActivity extends Fragment {


    private View view;
    private ArrayList ArticoloSelezionati = new ArrayList();

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = (View) inflater.inflate(R.layout.activity_rapportini, container, false);
        return view;
    }

    @Override
    public void onViewCreated(final View view, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final Button btnGeneraRapportino = (Button) view.findViewById(R.id.buttongenera_rapportino);
        final Button btnAggiungiArticoli = (Button) view.findViewById(R.id.button_inserimento_articoli);
        final EditText txtNote = (EditText) view.findViewById(R.id.EditText_Note);


        btnAggiungiArticoli.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                Intent articoliintent = new Intent(RapportiniActivity.this.getActivity(), ArticoliActivity.class);
                startActivity(articoliintent);

            }
        });

        //Configuro la funzione Listener sul login button
        btnGeneraRapportino.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                //Prima di procedere con la generazione del rapportino è necessario controllare che le note siano inserite
                if (txtNote.getText().toString().length() > 0) {
                    Rapportino r = new Rapportino();
                    String ImageBase64 = r.GeneraRapportinoPrivato(txtNote.getText().toString());
                    try {
                        //byte[] decodedString = Base64.decode(ImageBase64, Base64.DEFAULT);
                        //System.out.println("\n Decode String: "+decodedString);
                        //Bitmap bp = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                        //img.setImageBitmap(bp);
                    } catch (Exception ex) {
                        System.out.print("Errore: " + ex);
                    }
                } else {
                    Support.Notification(RapportiniActivity.this.getActivity(), "Attenzione", "Non hai inserito le note");
                }
            }
        });


    }


    /*  Tipologie di result code:
         1) Articolo

    */

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 1) {
            System.out.println("\n Hai selezionato un articolo");
            Articolo a = (Articolo) data.getSerializableExtra("articoloselezionato");
            ArticoloSelezionati.add(a);

        }
        System.out.println("\n Articolo selezionati: " + ArticoloSelezionati.size());
    }


}

in your code:

Intent articoliintent = new Intent(RapportiniActivity.this.getActivity(), ArticoliActivity.class);
startActivity(articoliintent);

use startActivityForResult

This is quite simple. Just make sure that the activity containing RapportiniActivity (which is a fragment), has onActivityResult overridden in it like this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
}

And your code should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM