How to pass data between fragments

后端 未结 13 3019
醉话见心
醉话见心 2020-11-22 07:03

Im trying to pass data between two fragmens in my program. Its just a simple string that is stored in the List. The List is made public in fragments A, and when the user cli

13条回答
  •  失恋的感觉
    2020-11-22 07:32

    Why don't you use a Bundle. From your first fragment, here's how to set it up:

    Fragment fragment = new Fragment();
    Bundle bundle = new Bundle();
    bundle.putInt(key, value);
    fragment.setArguments(bundle);
    

    Then in your second Fragment, retrieve the data using:

    Bundle bundle = this.getArguments();
    int myInt = bundle.getInt(key, defaultValue);
    

    Bundle has put methods for lots of data types. Please see http://developer.android.com/reference/android/os/Bundle.html

提交回复
热议问题