Question Detail

How to send String Activity to Fragment?

6 years ago Views 1350 Visit Post Reply

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


Thread Reply

Anonymous

- 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();
fragment.setArguments(bundle);

and in Fragment onCreateView method:

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