Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to transfer extra data from one fragment to another through activity

For example, In a list view hosted by ListActivity, when user click on a item on the list, a new activity will be launched, and previous activity transfer extra data to the new activity like below:

public class Notepadv2 extends ListActivity {
    ...

   @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Intent i = new Intent(this, NoteEdit.class);
        i.putExtra(NotesDbAdapter.KEY_ROWID, id);
        startActivityForResult(i, ACTIVITY_EDIT);
     }
}

How it should be If I use fragments? I mean if I have one Activity which host 2 fragments, and make fragments transactions like below:

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

how can I transfer extra data from one fragment to the other fragment through the host activity?

I know on android developer webpage, there is a good document of how to use fragment and how to communicate with activity, but there is no description about how to transfer data from one fragment to another....

like image 414
Leem.fin Avatar asked Feb 03 '12 11:02

Leem.fin


People also ask

How can we send some data from one fragment to another?

So to share a string between fragments you can declare a static String in Activity. Access that string from Fragment A to set the value and Get the string value in fragment B. 2. Both fragments are hosted by different Activities- Then you can use putExtra to pass a string from Fragment A of Activity A to Activity B.

How can you pass data from an activity to a fragment?

So, to pass data from the MotherActivity to such a Fragment you will need to create private Strings/Bundles above the onCreate of your Mother activity - which you can fill with the data you want to pass to the fragments, and pass them on via a method created after the onCreate (here called getMyData()).

How will you pass data from one fragment to another fragment in Android using interface?

To pass data from one fragment to another Bundle will help. LifeShapeDetailsFragment fragment = new LifeShapeDetailsFragment(); // object of next fragment Bundle bundle = new Bundle(); bundle. putInt("position", id); fragment. setArguments(bundle);


2 Answers

Use

Bundle data = new Bundle();
data.putString("name",value);
Fragment fragment = new nameOfFragment();
fragment.setArguments(data);
.navigateTo(fragment);
like image 79
Its not blank Avatar answered Oct 13 '22 01:10

Its not blank


From Activity you send data with intent as:

Bundle bundle = new Bundle();
bundle.putString("key", "value");
// set Fragmentclass Arguments
Fragmentclass fragmentobj = new Fragmentclass();
fragmentobj.setArguments(bundle);

and in Fragment onCreateView method:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String strtext = getArguments().getString("key");    
    return inflater.inflate(R.layout.fragment, container, false);
}

I hope it is help full to you.

like image 31
Neeraj Singh Avatar answered Oct 13 '22 00:10

Neeraj Singh