Send String from an Activity to a Fragment of another Activity

android, android-activity, android-fragments, android-intent

Solution

First, you'll actually send that string to your activity B. For example:

Intent intent = new Intent(this, YourActivityClass.class);
intent.putExtra("myString", "this is your string");
startActivity(intent);

then later read that string from your activity B and inject into your fragment before executing the fragment-transaction. For example:

Bundle args = new Bundle();
args.putString("myString", getIntent().getExtras().getString("myString"))
yourFragment.setArguments(args);

Later, use `getArguments()` in your fragment to retrieve that bundle.

Or alternatively, use the following in your fragment to directly access the activity intent and fetch your required value:

String str = getActivity().getIntent().getStringExtra("myString");

For more info, read this.

Problem

I have two Activities (A and B) and a Fragment F The Fragment F is contained in the Activity B I'd like to send Strings from Activity A to Fragment F How can do that? Thanks!

Original source