Activity和Fragment之间的通信用于发送值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Activity和Fragment之间的通信用于发送值相关的知识,希望对你有一定的参考价值。

大家好,然后你在下面看到的程序我应该执行这一系列的步骤:

1)Fragment1(Rapportini)我去Activity2(文章)

2)选择一个元素并返回Fragment1(Rapportini),将值作为intent传递

3)OnActivityResult方法读取intent并将其添加到arraylist

我的问题是没有调用OnActivityResult方法!为什么会这样,我该如何解决?

活动2(文章) - 代码:

  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("
  Hai fatto tap  
");
                finish();

片段结果(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("
 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("
 Hai selezionato un articolo");
            Articolo a = (Articolo) data.getSerializableExtra("articoloselezionato");
            ArticoloSelezionati.add(a);

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


}
答案

在你的代码中:

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

使用startActivityForResult

另一答案

这很简单。只要确保包含RapportiniActivity(这是一个片段)的活动,就像这样覆盖了onActivityResult

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

你的代码应该可行。

以上是关于Activity和Fragment之间的通信用于发送值的主要内容,如果未能解决你的问题,请参考以下文章

android Fragment与Activity交互,互相发数据(附图具体解释)

Android:Fragment与Activity之间的通信方式简单介绍

Android 中Fragment和Activity之间的通信

android--------实现Activity和Fragment通信的面向对象的万能接口

是否最好使用 Activity.onAttachFragment 或 Fragment.onAttach 在 Activity 和嵌套片段之间进行通信?

Activity与Fragment之间的通信