Updated 30 December 2016
We Know how to send the data from the Activity to another Activity. But what if we want to send the data from Activity to fragment?
So the technique is very similar to send data to activity. For sending the data to fragment we use the Bundle.
Bundles: A mapping from String keys to various Parcelable
values. They are generally used for passing data between various Android activities and fragments.
There are simple blocks of code to pass data from the Activity to fragments.
Step 1: Passing the data from activity to fragment,
12345 Bundle bundle = new Bundle();bundle.putString("params", "My String data");// set MyFragment ArgumentsMyFragment myObj = new MyFragment();myObj.setArguments(bundle);
Step 2: Receiving the data to the fragment,
123456 public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);if (getArguments() != null) {mParam1 = getArguments().getString("params");}}
Source: https://developer.android.com/
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
8 comments
Thx
Then you can pass on the bundle in the arguments while adding or initializing the fragment.
The code explained in the article itself explains the approach.