LuxuryWaffles
LuxuryWaffles

Reputation: 1708

Passing data from fragment in an Activity from Another Activity

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");
    }

Upvotes: 4

Views: 295

Answers (2)

s.d
s.d

Reputation: 29436

  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.

Upvotes: 0

AndroidLearner
AndroidLearner

Reputation: 4636

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.

Upvotes: 3

Related Questions