Reputation: 1708
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
Reputation: 29436
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.
Find an existing fragment by its id or tag: getFragmentManager.findFragmentById()
, and call fragment class's method.
Save parent activity reference to a local variable in onAttach()
of fragment and call parent activity class's methods from fragment.
Upvotes: 0
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