4

I have researched on passing data from Activity to Activity. But I could not use the same method to pass to my fragment. How do I pass the data to my fragment?

The Code I am using is

    Intent i = new Intent(getApplicationContext(), NewActivity.class);
    i.putExtra("new_variable_name","value");
    startActivity(i);

and In the other activity

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String value = extras.getString("new_variable_name");
    }
LuxuryWaffles
  • 1,518
  • 4
  • 27
  • 50

2 Answers2

3

Its pretty easy to pass data between two fragments with the help of interface.

I think this is what you are looking for .

Hope this helps :)

To pass the date from activity to fragment,it is nicely explained here.

Community
  • 1
  • 1
AndroidLearner
  • 4,500
  • 4
  • 31
  • 62
  • useful article, could use this later perhaps, could i use this for passing data from an activity to fragment? – LuxuryWaffles Jun 17 '13 at 06:58
  • I tried adding a getActivity() and it works, but I am new to android development so is there any recommended ways of doing things? – LuxuryWaffles Jun 17 '13 at 07:05
  • @BloopieBloops The way you are using now is correct but I Suggest you to keep this two things separate i.e. activity to fragment and fragment to fragment and if my amswer is helpful to you then don't forget to accept it ... – AndroidLearner Jun 17 '13 at 07:18
0
  1. Use setArguments() to pass a Bundle to a Fragment. Fragment will retain this bundle even on configuration change. It will be delivered to onCreate() and other methods of fragment.

  2. Find an existing fragment by its id or tag: getFragmentManager.findFragmentById(), and call fragment class's method.

  3. Save parent activity reference to a local variable in onAttach() of fragment and call parent activity class's methods from fragment.

S.D.
  • 29,290
  • 3
  • 79
  • 130