Question Detail

How to send String Activity to Fragment?

6 years ago Views 1369 Visit Post Reply

I want to pass data Activity to Fragment. How can I do this? 

Thread Reply


- 6 years ago

From Activity you send data with intent as:

String str="This is my Message";
Bundle bundle = new Bundle();
bundle.putString("message", str);
// set Fragmentclass Arguments
Fragmentclass fragment = new Fragmentclass();

and in Fragment onCreateView method:

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    String strtext = getArguments().getString("message");    
    return inflater.inflate(R.layout.fragment_lay, container, false);