I know that data can be transferred from an Activity to another Fragment using Bundle. I have done some research and I found out that Bundle works only when the Activity commits a Fragment transaction to the Fragment where the data has to be send.
My question is: Is there any way by which I can send data from the activity to another fragment without committing fragment transaction?
I have attached an image which explains the scenario:
Here is the Main Activity (User.java):
protected void onCreate(Bundle savedInstanceState)
{
ActionBar ab = getSupportActionBar();
ab.hide();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
TextView txt = (TextView) findViewById(R.id.textView5);
Intent intent = getIntent();
String Name = intent.getStringExtra("UserName");
txt.setText("Logged in as "+Name);
Bundle bundle = new Bundle();
bundle.putString("message", Name );
user_profile up = new user_profile(); //This is the Fragment where I want to send data(which is "Name")
up.setArguments(bundle);
user_home uh = new user_home(); //This is the Fragment that is added
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_container,uh);
ft.commit();
}
Here is the Second Activity where I want to send data(Known as "user_profile.java")
Bundle bundle2 = this.getArguments();
String N = bundle2.getString("message");
Toast.makeText(getContext(), N , Toast.LENGTH_SHORT).show();
Is there any way by which I can send between activity and fragment other than using bundle?