简体   繁体   中英

Populate spinner from arraylist in separate fragment

I have an app with 3 tabs. There's a main activity, and a fragment for each tab.

In fragment 1 (the first tab) I am able to add/remove items from a list view (stored in an arraylist). In fragment 2 (the second tab) I want to populate a spinner from the arraylist in the first tab; however I notice that I am unable to access objects from separate fragments (whether private, protected, or public).

What's the best way to pass the arraylist between fragments?

Fragment-to-fragment communication should go through the hosting Activity. Pass data up to the Activity from fragment1 (probably via a custom interface, to allow easy re-use of the Fragment), and then from the Activity into fragment2. See this section of the documentation for more details:

Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.

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