Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass data between fragments

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 clicks on a list item, I need it to show up in fragment B. The content provider only seems to support ID's, so that will not work. Any suggestions?

like image 401
Shaun Avatar asked Mar 04 '11 13:03

Shaun


1 Answers

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

like image 66
Lendl Leyba Avatar answered Sep 27 '22 22:09

Lendl Leyba